80

I need to access list names inside the lapply function. I've found some threads online where it's said I should iterate through the names of the list to be able to fetch each list element name in my function:

> n = names(mylist)
> mynewlist = lapply(n, function(nameindex, mylist) { return(mylist[[nameindex]]) }, mylist)
> names(mynewlist)
NULL
> names(mynewlist) = n

The problem is that mynewlist loses the original mylist indexes and I have to add that last names() assignment to restore them.

Is there a way to give an explicit index name to each element returned by the lapply function? Or a different way to make sure mynewlist elements have the correct index names set? I feel mynewlist index names could be wrong if lapply does not return the list elements in the same order than mylist.

Robert Kubrick
  • 8,413
  • 13
  • 59
  • 91
  • I keep coming back to this post. Following the answers below, you can directly assign the list items as names via ```n <- names(mylist) %>% `names<-`(c(.))``` after loading dplyr. – MartijnVanAttekum Mar 12 '19 at 21:26

6 Answers6

84

I believe that lapply by default keeps the names attribute of whatever you are iterating over. When you store the names of myList in n, that vector no longer has any "names". So if you add that back in via,

names(n) <- names(myList)

and the use lapply as before, you should get the desired result.

Edit

My brains a bit foggy this morning. Here's another, perhaps more convenient, option:

sapply(n,FUN = ...,simplify = FALSE,USE.NAMES = TRUE)

I was groping about, confused that lapply didn't have a USE.NAMES argument, and then I actually looked at the code for sapply and realized I was being silly, and this was probably a better way to go.

joran
  • 169,992
  • 32
  • 429
  • 468
  • Yep, this works. I still have to create 'n' though via n = names(myList). Two calls to names(myList), once to create n, the second to set n attributes. – Robert Kubrick Feb 27 '12 at 18:31
  • 3
    You could replace the second with `names(n) <- n` though. – Aaron left Stack Overflow Feb 27 '12 at 18:43
  • 1
    @RobertKubrick See my edit for a possibly nicer solution. Examine the code for `sapply` to see just how simple this is; it's just acting as a wrapper that adds the names in after the fact. – joran Feb 27 '12 at 18:51
  • 5
    @joran I used sapply and was able to output. But could you explain why you said "and then I actually looked at the code for `sapply` and realized I was being silly"? So why doesn't `lapply` have `USE.NAMES`? – Heisenberg Nov 01 '13 at 03:35
  • 1
    I had been avoiding `sapply` because of the unpredictable type changing, but, looking at `sapply` source, it seems it is safe if `simplify` is `FALSE`. E.g. see https://stackoverflow.com/questions/12339650/why-is-vapply-safer-than-sapply?rq=1 Thanks! – Jack Wasey Feb 25 '19 at 16:45
52

the setNames function is a useful shortcut here

mylist <- list(a = TRUE, foo = LETTERS[1:3], baz = 1:5)
n <- names(mylist)
mynewlist <- lapply(setNames(n, n), function(nameindex) {mylist[[nameindex]]})

which preserves the names

> mynewlist
$a
[1] TRUE

$foo
[1] "A" "B" "C"

$baz
[1] 1 2 3 4 5
Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
Brian Diggs
  • 57,757
  • 13
  • 166
  • 188
8

imap() from the purrr package is nice for your problem.

library(purrr)
mylist <- list(foo1=1:10,foo2=11:20)
imap(mylist, function(x, y) mean(x)) ## x is the value, y is the name

or you can use a more compact version of imap:

imap(mylist, ~ mean(.x))

Note that you can use variations of imap_xxx depending on the type of vector you want:

imap_dbl(mylist, ~ mean(.x)) ## will return a named numeric vector. 
Kevin Zarca
  • 2,572
  • 1
  • 18
  • 18
7

Building on joran's answer, and precising it:

The sapply(USE.NAMES=T) wrapper will indeed set as names of the final result the values of the vector you are iterating over (and not its names attribute like lapply), but only if these are characters.

As a result, passing indices will not help. If you want to pass indexes with sapply, you need to resort to some (ugly) casting:

sapply(as.character(c(1,11)), function(i) TEST[[as.numeric(i)]], USE.NAMES = TRUE)

In this case, a cleaner solution is to directly set and use names of your original object. Here is an exhaustive list of solutions:

TEST <- as.list(LETTERS[1:12])

### lapply ##
## Not working because no name attribute
lapply(c(1,11), function(i) TEST[[i]])

## working but cumbersome
index <- c(1,11)
names(index) <- index
lapply(index, function(i) TEST[[i]])

### sapply ##
## Not working because vector elements are not strings
sapply(c(1,11), function(i) TEST[[i]], simplify = F) 

## Working with the casting trick
sapply(as.character(c(1,11)), function(i) TEST[[as.numeric(i)]], simplify = F)

## Cleaner, using names with sapply:
names(TEST) <- LETTERS[26:15] 
sapply(names(TEST)[c(1,11)], function(name) TEST[[name]], simplify = F) 
Antoine Lizée
  • 3,743
  • 1
  • 26
  • 36
6

Have you looked into llply() from the package plyr?

It does exactly what you are asking for. For each element of a list, apply function, keeping results as a list. llply is equivalent to lapply except that it will preserve labels and can display a progress bar. from ?llply

mylist <- list(foo1=1:10,foo2=11:20)
>names(mylist)
[1] "foo1" "foo2"
newlist<- llply(mylist, function(x) mean(x))

>names(newlist)
[1] "foo1" "foo2"
Maiasaura
  • 32,226
  • 27
  • 104
  • 108
1

Also building on @joran's answer, you can write a wrapper function that preserves object attributes such as below:

lapply_preserve_names <- function(list, fun){
  lapply(seq_along(list), function(i) {
    obj = list[i]
    names(obj) = names(list)[i]
    fun(obj)
  })
}

then instead of using lapply, simply use lapply_preserve_names(your_list, function)

ChrisMM
  • 8,448
  • 13
  • 29
  • 48
Zhou Fang
  • 11
  • 1