When using the R *apply
functions, sometime one would like to access to the names of the processed elements inside the apply processing function. To make it clear, consider first this for-loop:
df=data.frame(x=1:3, y=11:13)
for(i in 1:ncol(df)){
cat('processing col', names(df)[i], '\n')
print(df[[i]])
}
# processing col x
# [1] 1 2 3
# processing col y
# [1] 11 12 13
When moving to the apply version:
lapply(df, function(col){
## processing col ??
col})
# $x
# [1] 1 2 3
#
# $y
# [1] 11 12 13
how to access the column names from inside the anonymous apply function?
This can't be done, except how you've done it with for
. If you want to return a structure with the results (and would be appending to a list, for example), *apply
may still be appropriate.
x <- lapply(names(iris), function(name) {
cat("processing col", name, "\n")
iris[[name]]
})
## processing col Sepal.Length
## processing col Sepal.Width
## processing col Petal.Length
## processing col Petal.Width
## processing col Species
names(x) <- names(iris)
x
is now a list containing the columns of the data frame iris
.