On Fri, Mar 20, 2009 at 8:18 PM, science! <karthik....@gmail.com> wrote: > I am aware that it is easily possible to create var names on the fly. e.g. > assign(paste("m",i,sep=""),j) > but is it possible to assign dataframes to variables created on the fly? > > e.g. > If I have a dataframe called master and I wanted to subset parts of those > data into separate dataframes, I could do: > > m1=subset(master,master$SAMPLE=='1') > m2=subset(master,master$SAMPLE=='2') > ..... > > but I would like to do this in a loop. Can someone give me suggestions on > how to accomplish this?
Assuming this is a good idea (a priori I am sceptical), the following works just fine. What problems did you run into? > master <- data.frame(a=sample(3,8,replace=TRUE),b=rnorm(8)) > for (i in 1:3) assign(paste("m",i,sep=""),subset(master,a==i)) > m1 a b 1 1 0.01395752 3 1 0.58781916 4 1 2.30619952 > m2 a b 5 2 -0.21210500 7 2 -1.12641675 8 2 0.01753290 > m3 a b 2 3 -0.957271 6 3 2.035773 ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.