[R] How to split a data table into multiple tables based on column (unique value) and save them separately

Ista Zahn istazahn at gmail.com
Thu Jan 22 19:10:22 CET 2015


The answer is right there in your question: see ?split

Best,
Ista
On Jan 22, 2015 12:55 PM, "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,
> -8L))
> 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.
> KG
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list