Suppose you have a list of character vectors:
set.seed(42)
x <- lapply(sample(10:50), function(x) {
sapply(1:x, function(y) {
paste(sapply(y, sample, x=letters, size=sample(5:10, 1)), collapse='')
})
})
You would like to apply a vectorized operation across this entire list. One option is to unlist(x)
, and then apply the vectorized function:
y <- nchar(unlist(x))
What is the most efficient way to get y
back into the same list structure as x
? Is there a better way to approach this problem?