[R] merging dataframes in a list

Ed Siefker ebs15242 at gmail.com
Fri Jun 3 21:09:49 CEST 2016


aggregate isn't really what I want.  Maybe tapply?  I still can't get
it to work.

> length(mylist)
[1] 4
> length(names)
[1] 4
> tapply(mylist, names, merge)
Error in tapply(mylist, names, merge) : arguments must have same length

I guess because a list isn't an atomic data type.  What function will
do the same on lists?  lapply doesn't have a 'by' argument.

On Fri, Jun 3, 2016 at 1:41 PM, Ed Siefker <ebs15242 at gmail.com> wrote:
> I manually constructed the list of sample names and tried the
> aggregate call I mentioned.
> Merge works when called manually, but not when using aggregate.
>
>> mylist <- list(data.frame(name="sample1", red=20), data.frame(name="sample1", green=15), data.frame(name="sample2", red=10), data.frame(na me="sample2", green=30))
>>  names <- list("sample1", "sample1", "sample2", "sample2")
>> merge(mylist[1], mylist[2])
>      name red green
> 1 sample1  20    15
>> merge(mylist[3], mylist[4])
>      name red green
> 1 sample2  10    30
>> aggregate(mylist, by=as.list(names), merge)
> Error in as.data.frame(y) : argument "y" is missing, with no default
>
> What's the right way to do this?
>
> On Fri, Jun 3, 2016 at 1:20 PM, Ed Siefker <ebs15242 at gmail.com> wrote:
>> 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?



More information about the R-help mailing list