Is there a way to get the list index name in my lapply() function?
n = names(mylist)
lapply(mylist, function(list.elem) { cat(\"What is the name of this list
Just write your own custom lapply
function
lapply2 <- function(X, FUN){
if( length(formals(FUN)) == 1 ){
# No index passed - use normal lapply
R = lapply(X, FUN)
}else{
# Index passed
R = lapply(seq_along(X), FUN=function(i){
FUN(X[[i]], i)
})
}
# Set names
names(R) = names(X)
return(R)
}
Then use like this:
lapply2(letters, function(x, i) paste(x, i))