Many R textbooks encourage the use of $ to retrieve variables (columns) from data.frames^. However, I found that this does not work inside a function, and I can\'t figure out wh
Personally I think the dollar operator $
is handy and useful from the R console. It permits completion and partial namings feature. $
is useful for an interactive mode . But if you want to use it within your function you should create a call using do.call
like this :
myFunc2 <- function(x, y){
z <- do.call('$',list(x,y))
return(z)
}
myFunc2(BOD,'demand')
[1] 8.3 10.3 19.0 16.0 15.6 19.8
But here is simpler to use [
as you have mentioned:
myFunc2 <- function(x, y){
z <- x[,y]
return(z)
}