I want to compute the mean of some variables using function. But the function I created did not work since it returned the warning "Unknown or uninitialised column: y.".
For the simple code, sub[sub$country == 'Germany',]$"ff_mean" works well, but test('ff') fails. I guess it is a problem on the variable quote within function, could you please let me know how I can solve this? Thanks!
Function:
test <- function(var) {
y <<- str_interp('${var}_mean')
mean_G <<- sub[sub$country == 'Germany',]$y
}
I also tried this, but still failed - Error: attempt to apply non-function
test <- function(var) {
y <<- str_interp('${var}_mean')
mean_G <<- sub[sub$country == 'Germany',]$eval(quote(y))
}