0

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

Community
  • 1
  • 1
I Del Toro
  • 913
  • 4
  • 15
  • 36
  • Looks like @BenBarnes is helping, but you probably get a better answer if you can make your example more reproducible (like using `head(dput(yourdf))` to give us example data). – Gregor Thomas Jan 29 '13 at 07:18

1 Answers1

2

Something like

for(species in unique(masterData$colA)) {
    this_file <- paste0(species, ".csv")   
    write.csv(x = masterData[masterData$colA == species, ], file = this_file)
}
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • Thank you. This seems to work with the test dataset. I will try again tomorrow with the real data and hope that it continues to work. Cheers. – I Del Toro Jan 29 '13 at 08:07
  • P.S. What happens if I want to sort the observations based on latitude in descending order and omit missing values and NAs in the same loop before writting out the CSV file? – I Del Toro Jan 29 '13 at 08:29
  • @user2020652 Shouldn't be a problem. Try it and see. – Gregor Thomas Jan 29 '13 at 17:57