[R] Assigning a successive set of values to newly created variables

arun smartpink111 at yahoo.com
Thu Jan 30 05:35:11 CET 2014


Hi,
May be this helps:

 set.seed(24)
 x<-rnorm(10)


vec1 <- seq(0.5,2,by=0.5)
for(i in 1:4){
 assign(paste("x",i,sep=""),x+vec1[i])
 }
A.K.


Hi all, I have been trying to figure this one out but I'm at a dead end. I want to write a loop that automatically create a series of values 
based on a previous variable. For example, I can initialize the first 
set of values for a variable "x" as 10 scores from a normal 
distribution. Then, I can create 4 new variables labeled x1, x2, x3, x4 
etc. Now, I want x1 to be the original x + a constant (say .5), x2 to be x1+.5, x3 to be x2+.5 etc. 

What I have so far accomplishes the first step (x1 as x+.5) but I
 can't figure out how to index the rest. Any help would be greatly 
appreciated! 

x<-rnorm(10) 
for(i in 1:4){ 
        assign(paste("x",i,sep=""),x+.5) 
        } 

p.s. I am trying to do this as a component of a function that 
will be generalized based on user input so the example above is a 
simplified version.



More information about the R-help mailing list