[R] Splitting a data frame into several completely separate data frames

Uwe Ligges ligges at statistik.tu-dortmund.de
Sun Sep 26 18:35:11 CEST 2010



On 26.09.2010 14:52, Josh B wrote:
> Hello again,
>
> How do I split a data frame into smaller, completely separate data frames
> (rather than separate data frames comprising a single "list")? Consider the
> following data, and my coding attempt:
>
> x<- read.table(textConnection("id type number
> indv.1 bagel 6
> indv.2 bagel 1
> indv.3 donuts 10
> indv.4 donuts 9"), header = TRUE)
> closeAllConnections()
>
> x.split<- split(x, x$type)


Although I doubt it is more convenient to have so many data.frames 
around (rather than having them nicely grouped in a list), you can 
simply do that in a loop:

for(i in names(x.split))
     assign(paste("nameOfDataFrame", i, sep="_"), temp[[i]])

Uwe Ligges


> This is where I'm stuck. Now I have one "list" comprised of different data
> frames, but what I want is separate data frames.
>
> Ideally, I'd like to design a loop to give sequentially-numbered names to the
> separate data frames I create. This is because my real data will have many more
> than two groups (i.e., many more types of things than just "bagels" versus
> "donuts") and the number of groups will vary when I apply the same code to
> different data sets.
>
> Many thanks in advance for your replies!
> -----------------------------------
> Josh Banta, Ph.D
> Center for Genomics and Systems Biology
> New York University
> 100 Washington Square East
> New York, NY 10003
> Tel: (212) 998-8465
> http://plantevolutionaryecology.org
>
>
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.



More information about the R-help mailing list