[R] with lapply() how can you retrieve the name of the object

From: Andrew Yee <yee_at_post.harvard.edu>
Date: Fri, 18 Jul 2008 18:21:15 -0400

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


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?


        [[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. Received on Fri 18 Jul 2008 - 22:24: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 18 Jul 2008 - 23:31:48 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