[R] For loop and using its index

Petr PIKAL petr.pikal at precheza.cz
Fri Mar 9 14:47:46 CET 2012


Hi

> Dear All,
> 
> I have a data set with variables x1, x2, x3, ..., x20 and I want to
> create z1, z2, z3, ..., z20 with the following formula:
> 
> 
> z1 = 200 - x1
> z2 = 200 - x2
> z3 = 200 - x3
> .
> .
> .
> z20 = 200 - x20.
> 
> 
> I tried using a for loop and its index as:
> 
> for (i in 1:20) {
> z(i) = 200 - x(i)
> }
> 
> But R gives the following error message: "Error: could not find function 
"x"".

You probably did not define any such function.

> 
> Is there any other way for a simple coding of my 20 lines of code?

No. Preferable is to do it in one line

z <- x-200

But it depends what is a set of variables. There is no such object in R 
AFAIK.

> PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


Regards
Petr

> 
> Alohas,
> Hassan Eini-Zinab
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list