Reputation: 32346
I have a list with named elements like so:
lst <- list(a1 = 5:12, b4 = c(34,12,5), c3 = 23:45)
I can easily retrieve the element names like so:
names(lst)
In a function I can loop over the list elements, but how can I access the name of the element that is being accessed in the loop:
test <- function(lst) {
for (l in lst) {
cat("The name of the current list element is", ???, "\n")
# Other processing of the list element
}
}
Upvotes: 2
Views: 1460
Reputation: 389235
purrr
's imap
(and iwalk
) are good functions if you want to access both data and the name of the list.
In base R, you may do this with any of the apply
function which has been already mentioned in comments. Here's one using Map
-
Map(function(x, y) sprintf('List name is %s and sum to %d', y, sum(x)),
lst, names(lst))
#$a1
#[1] "List name is a1 and sum to 68"
#$b4
#[1] "List name is b4 and sum to 51"
#$c3
#[1] "List name is c3 and sum to 782"
Upvotes: 1
Reputation: 102625
Maybe this could help
for (l in seq_along(lst)) {
cat("The name of the current list element is", names(lst[l]), "\n")
# Other processing of the list element
}
which gives
The name of the current list element is a1
The name of the current list element is b4
The name of the current list element is c3
Upvotes: 4
Reputation: 3899
In the purrr
package, there are the functions imap()
and iwalk()
. They take a list and a function with two arguments and apply the function to every element of the list and its index/name. The difference is, that iwalk
silently returns NULL
and is executed only for side effects (helpful if you map over cat()
) and imap()
works similar to lapply()
only it uses the functions second argument for the list's names.
library(purrr)
lst <- list(a1 = 5:12, b4 = c(34,12,5), c3 = 23:45)
imap(lst,\(x,y) cat("The name of the current list element is", y, "\n"))
#> The name of the current list element is a1
#> The name of the current list element is b4
#> The name of the current list element is c3
#> $a1
#> NULL
#>
#> $b4
#> NULL
#>
#> $c3
#> NULL
iwalk(lst,\(x,y) cat("The name of the current list element is", y, "\n"))
#> The name of the current list element is a1
#> The name of the current list element is b4
#> The name of the current list element is c3
Created on 2022-01-18 by the reprex package (v2.0.1)
Upvotes: 1