menno
2011-Jan-10 20:56 UTC
[R] How to save the output of "split" command as series of .csv files
Hello, I am looking for a way to quickly split a data frame containing daily temperature readings into a series of individual data frames, one for each year, in order to save them as individual .csv files which will be named according to Year. I would prefer not to use a series of "subset" commands given that there are a differing number of years for the various location. The data looks like this: Day Month Community Year MaxTemp MinTemp 1 1 '241552' 1990 44 29 2 1 '241552' 1990 39 20 3 1 '241552' 1990 35 24 . . . . . . . . . . . . 4 1 '241552' 1991 23 23 5 1 '241552' 1991 33 31 6 1 '241552' 1991 45 44 . . . . . . . . . . . . The "split" command (data2<-split(data, data$Year)) essentially does what I want, but I am unable to take the output from this command and save the data from individual years in separate files. I would ideally like to have a series of output files named "241552_1990", "241552_1991", etc. These data files will then be run through a loop that I have already created. I have attempted to feed the output of the split command directly into my pre-existing loop, but doing so will require major re-writes to my script due to column/row naming issues. Any information anyone could provide would be greatly appreciated. Thanks for your time, David Roth -- View this message in context: http://r.789695.n4.nabble.com/How-to-save-the-output-of-split-command-as-series-of-csv-files-tp3207964p3207964.html Sent from the R help mailing list archive at Nabble.com.
Peter Ehlers
2011-Jan-10 21:57 UTC
[R] How to save the output of "split" command as series of .csv files
On 2011-01-10 12:56, menno wrote:> > Hello, > > I am looking for a way to quickly split a data frame containing daily > temperature readings into a series of individual data frames, one for each > year, in order to save them as individual .csv files which will be named > according to Year. I would prefer not to use a series of "subset" commands > given that there are a differing number of years for the various location. > > The data looks like this: > > Day Month Community Year MaxTemp MinTemp > 1 1 '241552' 1990 44 29 > 2 1 '241552' 1990 39 20 > 3 1 '241552' 1990 35 24 > . . . . . . > . . . . . . > 4 1 '241552' 1991 23 23 > 5 1 '241552' 1991 33 31 > 6 1 '241552' 1991 45 44 > . . . . . . > . . . . . . > > > The "split" command (data2<-split(data, data$Year)) essentially does what > I want, but I am unable to take the output from this command and save the > data from individual years in separate files. I would ideally like to have a > series of output files named "241552_1990", "241552_1991", etc. These data > files will then be run through a loop that I have already created. I have > attempted to feed the output of the split command directly into my > pre-existing loop, but doing so will require major re-writes to my script > due to column/row naming issues. > > > Any information anyone could provide would be greatly appreciated. > > Thanks for your time, > > David Roth >How about a simple loop: for(n in names(data2)) write.table(data2[[n]], file = paste(dat2[[n]][, "Community"][1], "_", n, ".TXT", sep="")) Peter Ehlers> >
Dennis Murphy
2011-Jan-10 22:01 UTC
[R] How to save the output of "split" command as series of .csv files
Hi: This seems to work on the following toy example: df <- data.frame(gp = rep(LETTERS[1:6], each = 10), y = rnorm(60)) files <- paste('file', 1:6, '.csv', sep = '') mapply(write.csv, split(df, df$gp), file = files, MoreArgs = list(quote = FALSE, row.names = FALSE)) mapply() works here because you have two arguments that vary from one call to the next: the data subset in the list created by split() and the file name. The MoreArgs = part is to include function arguments that are the same from one call to the next. If you intend to split on more than one factor, then you should read the split() help page carefully first. On Mon, Jan 10, 2011 at 12:56 PM, menno <davroth@hotmail.com> wrote:> > Hello, > > I am looking for a way to quickly split a data frame containing daily > temperature readings into a series of individual data frames, one for each > year, in order to save them as individual .csv files which will be named > according to Year. I would prefer not to use a series of "subset" commands > given that there are a differing number of years for the various location. > > The data looks like this: > > Day Month Community Year MaxTemp MinTemp > 1 1 '241552' 1990 44 > 29 > 2 1 '241552' 1990 39 > 20 > 3 1 '241552' 1990 35 > 24 > . . . . . . > . . . . . . > 4 1 '241552' 1991 23 > 23 > 5 1 '241552' 1991 33 > 31 > 6 1 '241552' 1991 45 > 44 > . . . . . . > . . . . . . > > > The "split" command (data2<-split(data, data$Year)) essentially does what > I want, but I am unable to take the output from this command and save the > data from individual years in separate files. I would ideally like to have > a > series of output files named "241552_1990", "241552_1991", etc. These data > files will then be run through a loop that I have already created. I have > attempted to feed the output of the split command directly into my > pre-existing loop, but doing so will require major re-writes to my script > due to column/row naming issues. > > Loops? We don't need no steenking loops :)HTH, Dennis> > Any information anyone could provide would be greatly appreciated. > > Thanks for your time, > > David Roth > > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/How-to-save-the-output-of-split-command-as-series-of-csv-files-tp3207964p3207964.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org mailing list > 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]]