Possible Duplicate:
split dataframe into multiple output files in r
I need help in making a loop in R which will help me subset my master data file into individual csv files. My master file looks like this:
Master Dataset
colA= c("A", "A", "A", "B", "B", "B", "C", "C", "C")
colB= c(1.1,2.1,3.1,4.1,5.1,6.1,7.1,8.1,9.1)
colC= c(-1.2,-2.1,-3.1,-4.1,-5.1,-6.1,-7.1,-8.1,-9.1)
df= data.frame (spp=colA, latitude=colB, longitude=colC)
df
I need the outputs to have only the data for each species in a separate csv file so they look like this:
for species “A”…
colA= c("A", "A", "A")
colB= c(1.1,2.1,3.1)
colC= c(-1.2,-2.1,-3.1,)
df2= data.frame (spp=colA, latitude=colB, longitude=colC)
write.csv (df2, file= “spA.csv”)
The same format but in a separtate file for species “B” and "C"
It would be easy to do this for one or two species but my master matrix has over 400 species in it. Can anyone suggest how to write a loop that helps subset my data based on species? Thanks
Israel