2

I would like to iterate through a table and break it into relvant parts based on the number of visits. I have tried several things but cannot seem to get it to work.

I have included the code.

for(i in 1:6){
   paste("testing.visit",i,"\n",sep="") <- subset(testing,visit_no==2)
}

But I get the following error.

Error in paste("testing.visit", i, "\n", sep = "") <- subset(testing,  : 
  target of assignment expands to non-language object

Thank you,

Brandon

Marek
  • 49,472
  • 15
  • 99
  • 121
Brandon
  • 1,036
  • 2
  • 17
  • 19
  • Similar questions http://stackoverflow.com/questions/2679193/how-to-name-variables-on-the-fly-in-r, http://stackoverflow.com/questions/2590043/creating-a-series-of-vectors-from-a-vector – Marek May 25 '10 at 19:15

1 Answers1

4

Try assign inside the for loop:

assign(paste("testing.visit", i, "\n", sep=""),  subset(testing, visit_no==i))

You could also use dlply (plyr package ) to combine the subsets into a list:

library("plyr")
testing.visit <- dlply(testing, .(visit_no))
rcs
  • 67,191
  • 22
  • 172
  • 153