[R] passing (or obtaining) index or element name of list to FUN in lapply()

From: Stephen Tucker <brown_emu_at_yahoo.com>
Date: Tue, 12 Jun 2007 23:04:33 -0700 (PDT)

Hello everyone,

I wonder if there is a way to pass the index or name of a list to a user-specified function in lapply(). For instance, my desired effect is something like the output of

> L <- list(jack=4098,sape=4139)
> lapply(seq(along=L),function(i,x) if(i==1) "jack" else "sape",x=L)
[1] "jack"

[1] "sape"

> lapply(seq(along=L),function(i,x) if(names(x)[i]=="jack") 1 else 2,x=L)
[1] 1

[1] 2

But by passing L as the first argument of lapply(). I thought there was a tangentially-related post on this mailing list in the past but I don't recall that it was ever addressed directly (and I can't seem to find it now). The examples above are perfectly good alternatives especially if I wrap each of the lines in "names<-"() to return lists with appropriate names assigned, but it feels like I am essentially writing a FOR-LOOP - though I was surprised to find that speed-wise, it doesn't seem to make much of a difference (unless I have not selected a rigorous test):

> N <- 10000
> y <- runif(N)
## looping through elements of y
> system.time(lapply(y,

+                    function(x) {
+                      set.seed(222)
+                      mean(rnorm(1e4,x,1))
+                    }))

[1] 21.00 0.17 21.29 NA NA
## looping through indices
> system.time(lapply(1:N,
+                    function(x,y) {
+                      set.seed(222)
+                      mean(rnorm(1e4,y[x],1))
+                      },y=y))

[1] 21.09 0.14 21.26 NA NA

In Python, there are methods for Lists and Dictionaries called enumerate(), and iteritems(), respectively. Example applications:

## a list
L = ['a','b','c']
[x for x in enumerate(L)]
## returns index of list along with the list element [(0, 'a'), (1, 'b'), (2, 'c')]

## a dictionary
D = {'jack': 4098, 'sape': 4139}
[x for x in D.iteritems()]
## returns element key (name) along with element contents [('sape', 4139), ('jack', 4098)]

And this is something of the effect I was looking for...

Thanks to all,


R-help_at_stat.math.ethz.ch 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 Wed 13 Jun 2007 - 06:08:39 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 Wed 13 Jun 2007 - 08:32: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.