[R] merging dataframes in a list

ruipbarradas at sapo.pt ruipbarradas at sapo.pt
Fri Jun 3 21:17:07 CEST 2016


Hello,

Maybe something like the following.

lst <-
list(data.frame(name="sample1", red=20), data.frame(name="sample1",  
green=15), data.frame(name="sample2", red=10), data.frame(name="sample  
2", green=30))

fun <- function(DF){
    data.frame(name = DF[, 1], color = colnames(DF)[2], colnum = DF[, 2])
}
lst2 <- lapply(lst, fun)

result <- do.call(rbind, lst2)
result

Hope this helps,

Rui Barradas

 

Citando Ed Siefker <ebs15242 at gmail.com>:

> I have a list of data as follows.
>> list(data.frame(name="sample1", red=20), data.frame(name="sample1",  
>> green=15), data.frame(name="sample2", red=10),  
>> data.frame(name="sample 2", green=30))
>
> [[1]]
>     name red
> 1 sample1  20
>
> [[2]]
>     name green
> 1 sample1    15
>
> [[3]]
>     name red
> 1 sample2  10
>
> [[4]]
>     name green
> 1 sample2    30
>
> I would like to massage this into a data frame like this:
>
>     name red green
> 1 sample1  20    15
> 2 sample2  10    30
>
> I'm imagining I can use aggregate(mylist, by=samplenames, merge)
> right?  But how do I get the list of samplenames?  How do I subset
> each dataframe inside the list?
>
> ______________________________________________
> 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.htmland provide commented,  
> minimal, self-contained, reproducible code.

 

	[[alternative HTML version deleted]]



More information about the R-help mailing list