I am a new user to R
and I want to recall a block of code for different variables and I want to do something like this:
myfunc<- function(dat, groupvar, interestvar){
a<- dat%>% group_by(groupvar) %>% summarize(m=mean(interestvar))
..... other codes ....
return(a) }
myfunc(data=newdata, groupvar=variable1, interestvar=variable3)
myfunc(data=newdata, groupvar=variable2, interestvar=varaible4)
Here the variable1-vari
all are variables inside newdata
. It seems if I call intrestvar=newdata$varaible3
, that part will work. However, the way I call groupvar
still don't tell the function the group by is done by variable1
. How do I make it work?