[R] How to extract same columns from identical dataframes in a list?

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Mon Feb 8 17:26:38 CET 2016


Or

do.call( rbind, list.of.df )

from base R (without some of the robust behaviors that ldply implements).
-- 
Sent from my phone. Please excuse my brevity.

On February 8, 2016 7:33:54 AM PST, Ulrik Stervbo <ulrik.stervbo at gmail.com> wrote:
>Hi Wolfgang,
>
>I'm not sure exactly what you want, but the ldply in the package plyr
>can
>help you make a data.frame from a list of data.frames:
>
>library(plyr)
>
>dfa <- data.frame(cola = LETTERS[1:5], colb = c(1:5))
>dfb <- data.frame(cola = LETTERS[1:5], colb = c(1:5))
>
>df.lst <- list(dfa.name = dfa, dfb.name = dfb)
>
># If you want to use column number
>ldply(df.lst, function(cur.df){return(cur.df[, 2])})
>
># If the column name is always the same
>ldply(df.lst, function(cur.df){return(cur.df$colb)})
>
># Use the entire data.frame
>ldply(df.lst, function(cur.df){return(cur.df)})
>
># The latter can also be done with do.call
>do.call(rbind, df.lst)
>
>Hope this helps,
>Ulrik
>
>On Mon, 8 Feb 2016 at 16:07 Wolfgang Waser <waser at frankenfoerder-fg.de>
>wrote:
>
>> Hello,
>>
>> I have a list of 7 data frames, each data frame having 24 rows (hour
>of
>> the day) and 5 columns (weeks) with a total of 5 x 24 values
>>
>> I would like to combine all 7 columns of week 1 (and 2 ...) in a
>> separate data frame for hourly calculations, e.g.
>> > apply(new.data.frame,1,mean)
>>
>> In some way sapply (lapply) works, but I cannot directly select
>columns
>> of the original data frames in the list. As a workaround I have to
>> select a range of values:
>>
>> > sapply(list_of_dataframes,"[",1:24)
>>
>> Values 1:24 give the first column, 25:48 the second and so on.
>>
>> Is there an easier / more direct way to select for specific columns
>> instead of selecting a range of values, avoiding loops?
>>
>>
>> Cheers,
>>
>> Wolfgang
>>
>> ______________________________________________
>> 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.
>>
>
>	[[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.

	[[alternative HTML version deleted]]



More information about the R-help mailing list