I'm trying to use a generic function to operate on each column of a data frame where the operation will vary depending on the class of the column.
I'm having trouble giving the function access to the names of the columns while also dispatching the columns to the correct method.
df <- data.frame(f1 = factor(rep(1:3, 2)))
myfun <- function(x){
UseMethod("myfun", x)
}
myfun.factor <- function(x){
print("Using factor method")
print(names(x))
print(class(x))
}
myfun.default <- function(x){
print("Using default method")
print(names(x))
print(class(x))
}
Applying as a list gives the correct dispatch but strips the names from the columns
library(plyr)
l_ply(df, myfun)
[1] "Using factor method"
NULL
[1] "factor"
Applying as an array retains the names but doesn't give the correct names
a_ply(df, 2, myfun)
[1] "Using default method"
[1] "f1"
[1] "data.frame"
Is there a neat way to get the best of both or am I stuck with the method described in the answer to this question?