开发者

assign subset of parent table to objects in R [duplicate]

This question already has answers here: Create a variable name with "paste" in R? (4 answers) Closed 8 years ago.

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 o开发者_如何学Cbject

Thank you,

Brandon


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))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜