0

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

ila
  • 709
  • 4
  • 15
  • 1
    `log_transform` contains character values. It doesn't make sense to add `"var1"+1` which is why you are getting the error. When asking for help it is good to include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) that includes sample input data to your function and the desired output to test possible solutions. – MrFlick Jul 08 '15 at 17:54
  • It would appear that you're right. What's the best way to let the `log` command know that I'm trying to pass it variables and not quoted strings? Since they still need to be inputted into the function with quotes. And I'll work on some sample data, thanks – ila Jul 08 '15 at 18:02
  • Short answer: use `"[["`. See example below. – IRTFM Jul 08 '15 at 19:08

1 Answers1

0

No test cases provided so this remains untested but it was checked for syntactic completion and a right-curley-brace added. You still need to reference the columns by name within the component dataframes which your code was not doing:

function(df_list, log_transform = c("var1", "var2")) {
  if(!is.null(log_transform)) 
        {       #"If someone inputs a list of variables to be transformed, then..."
        trans <- function(df) {
            df[[ log_transform]] <- sapply(log_transform, function(x) { 
                   #"...apply a log transformation to each variable..."
                                                           log(df[[x]] + 1)
                                                                    } )
    llply(df_list, trans)
                 #"...for each dataframe in df_list."
                                    }
  #etc
             }                                      }
IRTFM
  • 258,963
  • 21
  • 364
  • 487