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.
Have you looked into
llply()
from the packageplyr
?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
imap()
from thepurrr
package is nice for your problem.or you can use a more compact version of imap:
Note that you can use variations of imap_xxx depending on the type of vector you want:
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:In this case, a cleaner solution is to directly set and use names of your original object. Here is an exhaustive list of solutions:
I believe that
lapply
by default keeps the names attribute of whatever you are iterating over. When you store the names ofmyList
inn
, that vector no longer has any "names". So if you add that back in via,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:
I was groping about, confused that
lapply
didn't have aUSE.NAMES
argument, and then I actually looked at the code forsapply
and realized I was being silly, and this was probably a better way to go.the
setNames
function is a useful shortcut herewhich preserves the names