[R] Creating dataframe names on the fly?
pauljohn32 at gmail.com
Sat Mar 21 07:15:04 CET 2009
On Fri, Mar 20, 2009 at 7:18 PM, science! <karthik.ram at gmail.com> wrote:
> I am aware that it is easily possible to create var names on the fly. e.g.
> but is it possible to assign dataframes to variables created on the fly?
> If I have a dataframe called master and I wanted to subset parts of those
> data into separate dataframes, I could do:
> but I would like to do this in a loop. Can someone give me suggestions on
> how to accomplish this?
> I tried assign(paste("m",i,sep=""),subset(master,master$SAMPLE==i) with no
Are you sure you really need to name these dataframes?
Here's a workaround that I use for these cases. Create your new data
frames and add them to a list, as in
myframes <- list(subset(master,master$SAMPLE=='1'),
Then when you want to use these things, you can get the first one as
You can name the objects inside the list:
names(myframes) <- c("A","B")
This is just as good as referring to them by name, in my experience.
It also has the benefit that because your dataframes are in a list,
then you can use features like lapply to do things for each dataset.
I'm reading ?assign now, and it appears you can actually name these things.
> name <- paste("fred",4,sep="")
> x <- data.frame(py=rnorm(10))
 "fred4" "mname" "name" "x"
Paul E. Johnson
Professor, Political Science
1541 Lilac Lane, Room 504
University of Kansas
More information about the R-help