[R] split dataframe to several dataframes in R

Bert Gunter gunter.berton at gene.com
Tue Feb 17 15:29:09 CET 2015


Inline.

-- Bert

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
Clifford Stoll




On Tue, Feb 17, 2015 at 1:33 AM, Chris Campbell
<ccampbell at mango-solutions.com> wrote:
> # Assuming you want to create many data frames, you can use
> # assign to create new objects.

But almost never should..

See ?list and read some tutorials -- Please!

> newDFNames <- unique(means$source)
> newDFNames
> # [1] "iris1.csv" "iris2.csv"
> for (nm in newDFNames) {
>      assign(x = nm,
>          value = means[means$source == nm, , drop = FALSE],
>          envir = .GlobalEnv)
> }
>
> iris1.csv
> #           Sepal.Length Sepal.Width Petal.Length Petal.Width    source
> # iris1.csv     5.843333    3.057333        3.758    1.199333 iris1.csv
>
> iris2.csv
> #           Sepal.Length Sepal.Width Petal.Length Petal.Width    source
> # iris2.csv     5.843333    3.057333        3.758    1.199333 iris2.csv
>
> # However, it may be that storing your
> # data objects as a single object, such as a list, is more useful.
>
> Chris Campbell, PhD
>
> Tel. +44 (0)1249 705 450 | Mobile. +44 (0) 7929 628 349
> www.mango-solutions.com
> Mango Solutions
> 2 Methuen Park, Chippenham, Wiltshire. SN14 OGB UK
>
> -----Original Message-----
> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Zilefac Elvis via R-help
> Sent: 17 February 2015 03:54
> To: R. Help
> Subject: [R] split dataframe to several dataframes in R
>
> Hi All,I have a dataframe called 'means' as shown below:iris1.csv <- iris iris2.csv <- iris names <- c("iris1.csv", "iris2.csv") dat <- mget(names)
> lst4 <- lapply(dat, function(x) apply(x[,-5], 2, mean))
>
> # Build the new data frame
> means <- as.data.frame(do.call(rbind, lst4)) means$source <- names(lst4) means
> #           Sepal.Length Sepal.Width Petal.Length Petal.Width       isv    source
> # iris1.csv     5.843333    3.057333        3.758    1.199333 0.3333333 iris1.csv
> # iris2.csv     5.843333    3.057333        3.758    1.199333 0.3333333 iris2.csvQUESTION: How can I split 'means' such that there are two files (dataframes) on my workspace:datframe 1#           Sepal.Length Sepal.Width Petal.Length Petal.Width       isv
> # iris1.csv     5.843333    3.057333        3.758    1.199333 0.3333333dataframe 2:#           Sepal.Length Sepal.Width Petal.Length Petal.Width       isv# iris2.csv     5.843333    3.057333        3.758    1.199333 0.3333333
> Many thanks,Asong.
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
>
> --
>
> LEGAL NOTICE\ \ This message is intended for the use of ...{{dropped:18}}
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.



More information about the R-help mailing list