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 <davr...@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]]

______________________________________________
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.

Reply via email to