[R] How to split a data table into multiple tables based on column (unique value) and save them separately
gunter.berton at gene.com
Thu Jan 22 19:16:31 CET 2015
You post here frequently.
1. Have you gone through any R tutorials (e.g. "An Intro to R", which
ships with R) where you would find out about how to handle such basic
operations? If not, why not??
2. Why do you persist in posting in HTML, even though you have been
asked not to? Not an issue here, particularly, but ...
2.5 But kudos on posting a small reproducible example that can be cut
3. To answer your question, either the base R idiom
lapply(split(...),FUN=..) or ?tapply handles this. ?by, ?aggregate,
and ?ave are wrappers that might be more convenient, so you might
start there. Hadley Wickham's widely used plyr package provides what
many consider to be much more usable interfaces and flexibility for
this sort of thing, so you may wish to learn and use this instead.
Genentech Nonclinical Biostatistics
"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
On Thu, Jan 22, 2015 at 9:52 AM, Kristi Glover
<kristi.glover at hotmail.com> wrote:
> How to split a data table into multiple tables based on column (unique value) and save them separately ?
> For example:
> dat<-structure(list(name = structure(c(1L, 1L, 1L, 2L, 2L, 3L, 3L,
> 4L), .Label = c("A", "B", "C", "D"), class = "factor"), date = structure(c(6L,
> 7L, 8L, 1L, 2L, 3L, 4L, 5L), .Label = c("5/10/2011", "5/11/2011",
> "5/12/2011", "5/13/2011", "5/14/2011", "5/7/2011", "5/8/2011",
> "5/9/2011"), class = "factor"), index = c(0.880538463, 0.256183209,
> 0.447239913, 0.942572251, 0.756441284, 0.098201146, 0.730044176,
> 0.177744529)), .Names = c("name", "date", "index"), class = "data.frame", row.names = c(NA,
> to make seperate table based on unique value, I did following way
> siteA<-subset(dat, dat$name=="A")
> write.csv(siteA, "siteA.csv")
> Similarly for site B,
> siteB<-subset(dat, dat$name=="B")
> write.csv(siteB, "siteB.csv")
> and so on
> But I have more than 800 unique numbers in the column "name". Therefore, I have to repeat it more than 800 times,
> I am wondering there is a quickest way to split the data and save automatically in csv format.
> Thanks for your help.
> [[alternative HTML version deleted]]
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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