tags:

views:

50

answers:

1

Hello, 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

+2  A: 

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