Hi I have a list which cosists out of dataframes of the same structure. Now I want to extract one column (let's say column "A") from all dataframes and have them in a matrix (or dataframe). At the moment I am doiong: d <- data.frame(A=runif(100), B=rnorm(100)) theList <- list(e1=d, e2=d, e3=d, e4=d) f <- sapply(theList, function(l){l$A} ) But I am sure ther is a more elegant way? Rainer -- Rainer M. Krug, Centre of Excellence for Invasion Biology, Stellenbosch University, South Africa [[alternative HTML version deleted]]
Henrique Dallazuanna
2009-Sep-16 11:35 UTC
[R] extracting one element from all list elements
Try this: sapply(theList, '[[', 'A') On Wed, Sep 16, 2009 at 8:34 AM, Rainer M Krug <r.m.krug at gmail.com> wrote:> Hi > > I have a list which cosists out of dataframes of the same structure. Now I > want to extract one column (let's say column "A") from all dataframes and > have them in a matrix (or dataframe). > > At the moment I am doiong: > > d <- data.frame(A=runif(100), B=rnorm(100)) > theList <- list(e1=d, e2=d, e3=d, e4=d) > f <- sapply(theList, function(l){l$A} ) > > But I am sure ther is a more elegant way? > > Rainer > > -- > Rainer M. Krug, Centre of Excellence for Invasion Biology, Stellenbosch > University, South Africa > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O