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

2016-02-10 Thread peter dalgaard
On 10 Feb 2016, at 10:04 , Wolfgang Waser wrote: > Hi, > > sapply(l,"[",T,2) > > and > > sapply(l, function(e) e[, 2]) > > > work fine! > > > Thanks a lot! > > Why is the second version "brute force and ignorance"? Is one of the > versions to be preferred? If

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

2016-02-10 Thread Bert Gunter
Google! (e.g. on "R Language tutorials") Some specific recommendations can be found here: https://www.rstudio.com/resources/training/online-learning/#R Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka

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

2016-02-10 Thread Wolfgang Waser
Hi, sapply(l,"[",T,2) and sapply(l, function(e) e[, 2]) work fine! Thanks a lot! Why is the second version "brute force and ignorance"? Is one of the versions to be preferred? If so, which and why (very briefly, please)? Results of the other options mentioned: > sapply(l,"[[",2)

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

2016-02-09 Thread S Ellison
ow to extract same columns from identical dataframes in a > list? > > Hi, > > sorry if my description was too short / unclear. > > > 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

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

2016-02-09 Thread Wolfgang Waser
Hi, sorry if my description was too short / unclear. > 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 [1] week1 week2 week3 ... 1 x a m ... 2 y b n 3

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

2016-02-09 Thread Ulrik Stervbo
Hi Wolfgang, If you use cbind in the do.call you get something close to what you want. You can then use grep to get just the columns you are interested in (I assume they all have the same name in the original data.frames) dfa <- data.frame(cola = c(6:10), colb = c(1:5), colc = c(11:15)) dfb <-

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

2016-02-09 Thread peter dalgaard
Like this? > l <- replicate(3,data.frame(w1=sample(1:4),w2=sample(1:4)), simplify=FALSE) > l [[1]] w1 w2 1 2 2 2 3 3 3 1 1 4 4 4 [[2]] w1 w2 1 3 4 2 2 2 3 1 3 4 4 1 [[3]] w1 w2 1 1 4 2 4 3 3 2 1 4 3 2 > sapply(l,"[[",2) [,1] [,2] [,3] [1,]244 [2,]

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

2016-02-08 Thread Jeff Newmiller
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 wrote: >Hi Wolfgang, > >I'm not sure exactly what you want,

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

2016-02-08 Thread Dénes Tóth
Hi, Although you did not provide any reproducible example, it seems you store the same type of values in your data.frames. If this is true, it is much more efficient to store your data in an array: mylist <- list(a = data.frame(week1 = rnorm(24), week2 = rnorm(24)), b =

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

2016-02-08 Thread Wolfgang Waser
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

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

2016-02-08 Thread Ulrik Stervbo
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,