I have created a dataset in R as follows:
m <- mtcars
m$dep<- ifelse(m$mpg <=16,1,0)
Now if I try to sum the variable dep as per the group done on the basis of cyl
a <-aggregate(dep_var~ cyl, FUN=sum, data=m)
a
I get the desired result. However, my problem is if I try to convert it into a user defined function to automate it, I am getting an error . I tried the following code:
f<- function(target,variable,data){
a <-aggregate(target ~ variable, FUN=sum, data=data)
return(a)
}
f(dep,cyl,m)
Could you please help me in this regard. Could you please also tell me when should I use double quotes while calling a function? Eg. f("dep","cyl",m). I tried this code for my function but it didn't work too.
Please some body help me to rectify the function.