I've got three lists with and each of them contains many tibbles. I want to name objects within the lists based on a particular value in the tibble. I know how to do it with one list at a time. To make things easier, I'll use vectors in my example:
library("purrr")
list1 <- list(c(id = "ABC", val=1), c(id = "DEF", val = 2), c(id = "GHI", val = 3))
list2 <- list(c(id = "JKL", val=4), c(id = "MNO", val = 5), c(id = "PRS", val = 6))
list3 <- list(c(id = "TUW", val=7), c(id = "XYZ", val = 8), c(id = "ZYX", val = 8))
names(list1) <- map_chr(list1, ~ .x[["id"]])
names(list2) <- map_chr(list2, ~ .x[["id"]])
names(list3) <- map_chr(list3, ~ .x[["id"]])
It works but I wonder if there's any bulk method of getting it done. I've tried using a for loop, but it didn't work, it creates separate i variable but doesn't rename objects in my lists:
for (i in c(list1, list2, list3)) {names(i) <- map_chr(i, ~ .x[["id"]])}
I know for loops are considered a bad practice in R so other suggestions would be much appreciated.