I am wondering if there is a way to directly return a data frame from a call, applyor plyrwhen returning from a function can have a variable number of columns (but will always have the same number of rows). For example:
df <- data.frame(A = 1:3, B = c("a","b", "c"))
my_fun <- function(x){
if(is.numeric(unlist(x))){
return(x)
} else {
return(cbind(x, x))
}
}
The closest I could get by returning the list and converting it to a data frame:
library(plyr)
data.frame(alply(df, 2, my_fun))
#
#
#
#
It seems like there should be a way to do this without additional conversion, is there?
source
share