Skip to content

For loop and using its index

5 messages · Hassan Eini Zinab, PIKAL Petr, Milan Bouchet-Valat +2 more

#
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"".

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

Alohas,
Hassan Eini-Zinab
#
Hi
"x"".

You probably did not define any such function.
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.
http://www.R-project.org/posting-guide.html
Regards
Petr
http://www.R-project.org/posting-guide.html
#
Le vendredi 09 mars 2012 ? 13:24 +0330, Hassan Eini Zinab a ?crit :
This is very basic, please read the R intro.

The problem is that x(i) means "call function x with argument i", and no
function x exists (nor z, BTW). You need
x1 <- 1:10
x2 <- 11:20

for (i in 1:2) {
    assign(paste("z", i, sep=""), 200 - get(paste("x", i, sep="")))
}

But you'd better use a data frame to store these variables, in which
case you can do:
df <- data.frame(x1=1:10, x2=11:20)
for (i in 1:2) {
    df[[paste("z", i, sep="")]] <- 200 - df[[paste("x", i, sep="")]]
}

You can also create a new data frame:
xdf <- data.frame(x1=1:10, x2=11:20)
zdf <- 200 - xdf
colnames(zdf) <- paste("z", 1:2, sep="")
df <- cbind(xdf, zdf)


Regards
#
On Fri, Mar 09, 2012 at 01:24:00PM +0330, Hassan Eini Zinab wrote:
Hi.

Try this.

  x <- 21:40
  z <- 200 - x
  x[1] # [1] 21
  x[2] # [1] 22
  z[1] # [1] 179
  z[2] # [1] 178

Hope this helps.

Petr Savicky.
#
Hassan, 

Others have provided you with better solutions, but I hope this allows you
to see why yours didn't work.

# first (going with your code) you needed a data.frame called "x" 
# here is an example:

x <- structure(list(x1 = c(0.0986048226696643, -0.445652024980979, 
0.0893989676314604, -3.02656448303247, -0.966125836458264,
-1.49916656636977, 
-1.43173455089552, 0.370528111260298, -1.16980816517156, -0.808744946153693
), x2 = c(-0.765406771195136, -0.37933377428095, 1.38846324586498, 
-1.70043724374807, -0.71331175577977, 1.44597103991061, 1.31674350467787, 
-0.954578441470943, -1.30637013925954, 0.551870274117374), x3 =
c(-0.122350075070145, 
1.6217818199546, -0.824570718637696, -0.0341988842898353, 1.03924814479596, 
0.898533448980663, 0.68074228601446, 0.296251937506574, -0.698501590358135, 
-0.0533564535030227)), .Names = c("x1", "x2", "x3"), row.names = c(NA, 
-10L), class = "data.frame")

# you then need an empty vector to hold the results of your for loop (called
"z" here)
# note the square brackets as opposed to your parentheses

z <- vector("list")
for (i in 1:ncol(x)) { 
z[i] = 200 - x[i] 
} 

# finally, this will reassemble the output of the for loop

z <- do.call(cbind, z)
colnames(z) <- c("z1", "z2", "z3")

# Finally, do read what the others sent you as they provide more efficient
solutions


HTH
Chuck







Hassan Eini Zinab wrote
--
View this message in context: http://r.789695.n4.nabble.com/For-loop-and-using-its-index-tp4459454p4460277.html
Sent from the R help mailing list archive at Nabble.com.