arun
2014-Jan-30 04:35 UTC
[R] Assigning a successive set of values to newly created variables
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.
arun
2014-Jan-30 04:45 UTC
[R] Assigning a successive set of values to newly created variables
Also: xNew <- x for(i in 1:4){ ?xNew <- xNew+0.5 ?assign(paste("y",i,sep=""),xNew) ?} identical(x2,y2) #[1] TRUE ?identical(x3,y3) #[1] TRUE A.K. On Wednesday, January 29, 2014 11:35 PM, arun <smartpink111 at yahoo.com> wrote: 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.