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

Kristi Glover kristi.glover at hotmail.com
Thu Jan 22 18:52:39 CET 2015


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]]



More information about the R-help mailing list