R users, I have a simple lapply question. g <- list(a=1:3, b=4:6, c=7:9) g <- lapply(g, function(x) as.data.frame(x)) lapply(g, function(x) cbind(x, var1 = rep(names(g), each=nrow(x))[1:nrow(x)])) I get $a x var1 1 1 a 2 2 a 3 3 a $b x var1 1 4 a 2 5 a 3 6 a $c x var1 1 7 a 2 8 a 3 9 a And I would like to have $a x var1 1 1 a 2 2 a 3 3 a $b x var1 1 4 b 2 5 b 3 6 b $c x var1 1 7 c 2 8 c 3 9 c How should I modify my lapply clause to achieve this? Best regards, Lauri> sessionInfo()R version 2.6.1 (2007-11-26) i386-apple-darwin8.10.1 locale: C attached base packages: [1] stats graphics grDevices utils datasets methods base
Try this: g <- list(a=1:3, b=4:6, c=7:9) with(stack(g), split(stack(g), ind)) On 21/02/2008, Lauri Nikkinen <lauri.nikkinen at iki.fi> wrote:> R users, > > I have a simple lapply question. > > g <- list(a=1:3, b=4:6, c=7:9) > g <- lapply(g, function(x) as.data.frame(x)) > lapply(g, function(x) cbind(x, var1 = rep(names(g), each=nrow(x))[1:nrow(x)])) > > I get > > $a > x var1 > 1 1 a > 2 2 a > 3 3 a > > $b > x var1 > 1 4 a > 2 5 a > 3 6 a > > $c > x var1 > 1 7 a > 2 8 a > 3 9 a > > And I would like to have > > $a > x var1 > 1 1 a > 2 2 a > 3 3 a > > $b > x var1 > 1 4 b > 2 5 b > 3 6 b > > $c > x var1 > 1 7 c > 2 8 c > 3 9 c > > How should I modify my lapply clause to achieve this? > > Best regards, > Lauri > > > sessionInfo() > R version 2.6.1 (2007-11-26) > i386-apple-darwin8.10.1 > > locale: > C > > attached base packages: > [1] stats graphics grDevices utils datasets methods base > > ______________________________________________ > 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
On Thursday 21 February 2008 (19:22:40), Lauri Nikkinen wrote:> R users, > > I have a simple lapply question. > > g <- list(a=1:3, b=4:6, c=7:9) > g <- lapply(g, function(x) as.data.frame(x))> And I would like to have > > $a > ? x var1 > 1 1 ? ?a > 2 2 ? ?a > 3 3 ? ?a > > $b > ? x var1 > 1 4 ? ?b > 2 5 ? ?b > 3 6 ? ?b > > $c > ? x var1 > 1 7 ? ?c > 2 8 ? ?c > 3 9 ? ?cThe following seems to work: sapply(names(g), function(n) cbind( g[[n]], var1 = rep(n,each=nrow(g[[n]])) ), simplify=FALSE) Best, Martin
I don't see any difference --- Lauri Nikkinen <lauri.nikkinen at iki.fi> wrote:> R users, > > I have a simple lapply question. > > g <- list(a=1:3, b=4:6, c=7:9) > g <- lapply(g, function(x) as.data.frame(x)) > lapply(g, function(x) cbind(x, var1 = rep(names(g), > each=nrow(x))[1:nrow(x)])) > > I get > > $a > x var1 > 1 1 a > 2 2 a > 3 3 a > > $b > x var1 > 1 4 a > 2 5 a > 3 6 a > > $c > x var1 > 1 7 a > 2 8 a > 3 9 a > > And I would like to have > > $a > x var1 > 1 1 a > 2 2 a > 3 3 a > > $b > x var1 > 1 4 b > 2 5 b > 3 6 b > > $c > x var1 > 1 7 c > 2 8 c > 3 9 c > > How should I modify my lapply clause to achieve > this? > > Best regards, > Lauri > > > sessionInfo() > R version 2.6.1 (2007-11-26) > i386-apple-darwin8.10.1 > > locale: > C > > attached base packages: > [1] stats graphics grDevices utils datasets > methods base > > ______________________________________________ > 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. >
Here is one way of doing it:> lapply(names(g), function(z)cbind(x=g[[z]], var1=z))[[1]] x var1 1 1 a 2 2 a 3 3 a [[2]] x var1 1 4 b 2 5 b 3 6 b [[3]] x var1 1 7 c 2 8 c 3 9 c On Thu, Feb 21, 2008 at 1:22 PM, Lauri Nikkinen <lauri.nikkinen at iki.fi> wrote:> R users, > > I have a simple lapply question. > > g <- list(a=1:3, b=4:6, c=7:9) > g <- lapply(g, function(x) as.data.frame(x)) > lapply(g, function(x) cbind(x, var1 = rep(names(g), each=nrow(x))[1:nrow(x)])) > > I get > > $a > x var1 > 1 1 a > 2 2 a > 3 3 a > > $b > x var1 > 1 4 a > 2 5 a > 3 6 a > > $c > x var1 > 1 7 a > 2 8 a > 3 9 a > > And I would like to have > > $a > x var1 > 1 1 a > 2 2 a > 3 3 a > > $b > x var1 > 1 4 b > 2 5 b > 3 6 b > > $c > x var1 > 1 7 c > 2 8 c > 3 9 c > > How should I modify my lapply clause to achieve this? > > Best regards, > Lauri > > > sessionInfo() > R version 2.6.1 (2007-11-26) > i386-apple-darwin8.10.1 > > locale: > C > > attached base packages: > [1] stats graphics grDevices utils datasets methods base > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?