0

I have a large data set(csv) of temperatures which I have to subset by date (years) to create 5 smaller ones by decade. So data from the years 1960-1969 will be one smaller csv file , data from years 1970-1979 will be another csv file etc. I've been trying to do this using a for loop

so something like this :

IDs<-unique(df$ID)
for (i in 1:length(IDs)){ 
  temp <- df[df$ID==IDs[i],]
  #more things to do with temp
}

But I'm not sure exactly how to go about this. The for loop should create the new csv files that have been subsetted by date from the existing large data set.

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
Ria
  • 1
  • 1
  • 2
    Please provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Roman Luštrik Nov 21 '15 at 19:56
  • 1
    For large datasets have a look at the package `data.table` and for fast reading there is the function `fread()`. – jogo Nov 21 '15 at 19:59
  • 1
    You can use `cut` to create the grouping variable and then `split` the dataset with that group. – akrun Nov 21 '15 at 20:00

1 Answers1

0

This seems pretty generic, and thus searchable. I did a Google search, and this was literally the first thing I found.

http://www.r-bloggers.com/splitting-a-large-csv-file-into-separate-smaller-files-based-on-values-within-a-specific-column/

I tried the script listed there and it worked perfect. Give it a go, and see how you get along.

ASH
  • 20,759
  • 19
  • 87
  • 200