简体   繁体   中英

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

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

The issue is that when you call the function list , mpg and am are not variables in the global environment. To do what you want, change the function signature to input the formula and call the function with the formula:

list <- function(f, dataset){
  return(summaryBy(f, data = dataset, FUN=fun))
}
table <- list(as.formula(mpg~am), mtcars)
print(table)
##  am mpg.FUN1 mpg.FUN2
##1  0 17.14737 3.833966
##2  1 24.39231 6.166504

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM