Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

Two things to note here: First: The for loop, doesn't have a return value, but the lapply does. Second: The lapply does not pass the column name, only the unnamed vector.

So something like this a better generic solution:

x <- lapply(1:ncol(df),function(i){
  cat('processing col', names(df)[i], '\n')
  print(df[[i]])
  })

You can later discard x (by rm(x)) if you don't need it.

Note that this will be easier if you have to replace a lot of for loops!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top