Skip to content
Back to formatted view

Raw Message

Message-ID: <1407106680.71150.YahooMailNeo@web160605.mail.bf1.yahoo.com>
Date: 2014-08-03T22:58:00Z
From: Zilefac Elvis
Subject: Convert some columns of List to dataframe R
In-Reply-To: <CAAJSdjgL588DfpV0s_A+_5L6c6XEvgx47yqWFt53FMHvYC3WHQ@mail.gmail.com>

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