I would like to create a defined function with dplyr in R. But I stuck in passing argument in mutate function. I have two datasets.
data1:
GROUP | AGEGRP | COUNT |
---|---|---|
1 | 0 | 15 |
2 | 1 | 20 |
data2:
GROUP | COUNT |
---|---|
3 | 15 |
My function is
freqcnt <- function(var) {
var <- enquo(var)
data2 <- data2 %>%
mutate(!!var = 99)
data1 <- data1 %>%
rbind(data2) %>%
return()
}
When I run the following code,
df <- freqcnt(AGEGRP)
the error message is popped up.
Error: unexpected '=' in:
"
mutate(!!var ="
However, if I remove !!, then var will become the name of variable instead of AGEGRP. Please show me some lights. Thanks,