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 e
the setNames function is a useful shortcut here
setNames
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