My question isn't quite as crazy as it sounds (hopefully). I have a function which takes as an input a list of dataframes, and gives as an output a list of corresponding linear models. One input to the function is the argument log_transform
. I'd like the user to be able to input a list of variables to be log transformed, and have that be taken into account by the model. This gets a little complex however, since this needs to be applied not only to multiple variables, but to multiple variables across multiple dataframes. As is, I have this coded as so:
function(df_list, log_transform = c("var1", "var2")) {
if(!is.null(log_transform)) { #"If someone inupts a list of variables to be transformed, then..."
trans <- function(df) {
sapply(log_transform, function(x) { #"...apply a log transformation to each variable..."
x <- log(x + 1)
}, simplify = T)
llply(df_list, trans) #"...for each dataframe in df_list."
}
etc
}
However, when I try to run this, I receive the error:
Error in x + 1 : non-numeric argument to binary operator
Where am I going wrong?
Thanks