[R] with lapply() how can you retrieve the name of the object
jim holtman
jholtman at gmail.com
Sat Jul 19 01:00:19 CEST 2008
forgot one step:
> z <- lapply(names(test.list), function(x){
+ cbind(test.list[[x]], var.2=x)
+ })
> names(z) <- names(test.list)
> z
$a
var.1 var.2
1 1 a
2 2 a
3 3 a
4 4 a
5 5 a
$b
var.1 var.2
1 11 b
2 12 b
3 13 b
4 14 b
5 15 b
>
On Fri, Jul 18, 2008 at 6:21 PM, Andrew Yee <yee at post.harvard.edu> wrote:
> In the following code, I'd like to be able to create a new variable
> containing the value of the names of the list.
>
>
> a <- data.frame(var.1 = 1:5)
> b <- data.frame(var.1 = 11:15)
>
> test.list <- list(a=a, b=b)
>
> # in this case, names(test.list) is "a" and "b"
>
> # and I'd like to use lapply() so that
> # I get something that looks like
> # var.1 var.2
> # 1 a
> # 2 a
> # 3 a
> #etc.
>
> new.list <- lapply(test.list, function(x) {x$var.2 <- names(x)
> x} )
>
>
> # the above clearly doesn't do it. How do you pull out the names of the
> thing that is being lapplied?
>
> Thanks,
> Andrew
>
> [[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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem you are trying to solve?
More information about the R-help
mailing list