0

I am trying to use the tapply function to get some output for a variable x.

a = c(1,2,3,4)
b = c(5,3,8,1)
c = c(1,1,2,2)
df = data.frame(a,b,c)

fun <- function(data,var){
x <- tapply(data$b,data$var,function(x) return(x))
return(x)
}

fun(df,c)

However, when I run this I get the following error:

 Error in tapply(data$b, data$var, function(x) return(x)) : 
 arguments must have same length 

I have already tried using the eval(parse(text = paste approach with no luck. Thanks for the help!

B. Woz
  • 33
  • 5
  • 1
    I highly recommend not using `tapply`. Use `group_by` from `dplyr` or `by` from `data.table`. In this setup, you can either do `fun(df, 'c')` and replace `data$var` with `data[[var]]`, or try `with(data, tapply(b, var, identity))` – MichaelChirico Apr 07 '17 at 00:01
  • Thank you! That's exactly what did the trick! – B. Woz Apr 07 '17 at 16:51

0 Answers0