Re: [R] How to get names of a list into df:s?

From: John Kane <jrkrideau_at_yahoo.ca>
Date: Thu, 21 Feb 2008 17:04:40 -0500 (EST)


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.
>



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. Received on Thu 21 Feb 2008 - 22:07:07 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 22 Feb 2008 - 01:30:16 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive