Search code examples
rfunctionsummary

Function for SummaryBy


I try to write a function to produce mean and sd

library(doBy)
fun = function(x){
  mean = mean(x, na.rm = TRUE)
  sd = sd(x, na.rm = TRUE)
  c(mean, sd)
  }

summaryBy(mpg~am, data = mtcars, FUN=fun)

It works call summaryBy, but when I try to put in function to call names of variable and dataset, it gives me error

"Error in list(mpg, am, mtcars) : (list) object cannot be coerced to type 'double"

list <- function(x,y,dataset){
  x <- as.numeric(x)
  y <- as.factor(y)
  table <- summaryBy(x~y, data = dataset, FUN=fun)
  table

}

list(mpg, am, mtcars)

Thanks for your advices


Solution

  • This has nothing to do with summaryBy, it's a mistake in the code for your list function. (Incidentally, you shouldn't name a function "list", because that is already the name of an important function in R and you're going to end up with problems.) Try this (you need to input the variable names in quotes):

    my.tab <- function(x, y, dataset){
      xn <- with(dataset, as.numeric(get(x)))
      yf <- with(dataset, as.factor(get(y)))
      newdf <- data.frame(xn=xn, yf=yf)
      names(newdf) <- c(x, y)
      table <- summaryBy(as.formula(paste0(x,"~",y)), data=newdf, FUN=fun)
      table
    }
    my.tab("mpg", "am", mtcars)
    #   am mpg.FUN1 mpg.FUN2
    # 1  0 17.14737 3.833966
    # 2  1 24.39231 6.166504