[R] Convert some columns of List to dataframe R

Zilefac Elvis zilefacelvis at yahoo.com
Mon Aug 4 00:58:00 CEST 2014


Hi John, 
Thanks. As you said, I want the "Site" values to become 
individual variable names whose value is in the "x" column: 
Output should look exactly like: 
Year G101 G102 
2001 33.1 34 
Thanks, 
Atem.



On Sunday, August 3, 2014 4:48 PM, John McKown <john.archie.mckown at gmail.com> wrote:
On Sun, Aug 3, 2014 at 5:43 PM, John McKown
<john.archie.mckown at gmail.com> wrote:
> On Sun, Aug 3, 2014 at 5:12 PM, Zilefac Elvis <zilefacelvis at yahoo.com> wrote:
>> Dear All,
>> I have a List in R and would like to convert it to data.frame.
>> Below is a reproducible example. I can do something like:
>>
>> x1<-do.call(cbind.data.frame, lst3) #OR
>> x1<-as.data.frame(lst3).
>>
>> However, my output looks like this:
>>
>> Year Site  x   Year Site  x
>> 2001 G101 33.1 2001 G102 34
>>
>> I would like to have as my output:
>>
>> Year  G101   G102
>> 2001  33.1   34
>>
>>
>> Thanks
>> Atem.
> <snip>
>
> Try rbind_all in the dplyr package.
>
> output <- rbind_all(as.list(lst3));
>
> Example transcript:
>
>> lst3 <- list(structure(list(Year = 2001L, Site = structure(1L, .Label = "G101", class = "factor"),
> +     x = 33.1), .Names = c("Year", "Site", "x"), row.names = c(NA,
> + -1L), class = "data.frame"), structure(list(Year = 2001L, Site =
> structure(1L, .Label = "G102", class = "factor"),
> +     x = 34), .Names = c("Year", "Site", "x"), row.names = c(NA,
> + -1L), class = "data.frame"))
>> library(dplyr);
>> rbind_all(lst3);
>   Year Site    x
> 1 2001 G101 33.1
> 2 2001 G102 34.0
> Warning message:
> In rbind_all(lst3) : Unequal factor levels: coercing to character

OOPS. Didn't got far enough. You want the "Site" values to become
individual variable names whose value is in the "x" column. Some days
I need more naps. But I think that this is the starting point. More
later. Sorry.

>
>
> --
> There is nothing more pleasant than traveling and meeting new people!
> Genghis Khan
>
> Maranatha! <><
> John McKown






-- 
There is nothing more pleasant than traveling and meeting new people!
Genghis Khan

Maranatha! <><
John McKown




More information about the R-help mailing list