Suppose I have a list nested in a list and I have a function that only works with vectors (for example, str_replace from the stringr package). A function should do its job on every element that actually entails information, ...
Question 1: Is there a specific solution to my problem?
Question 2: Is there a general solution?
There should be a solution using loops, but that's all, but elegant and probably very slow - efficiency plays a role here.
Let has an example :
# let start easy: test1 <- list(c("a","d"),c("b","d"),c("c","d")) # does not work: str_replace(test1,"d","changed") # but this does: lapply(test1,str_replace,"d","changed") # but what now ? test2 <- list(c(list("a"),"d"),c("b","d"),c("c","d")) # does not work! :-( lapply(test2,str_replace,"d","changed")
source share