Indexing a dataframe with $ inside a function?

后端 未结 3 733
生来不讨喜
生来不讨喜 2021-01-26 11:10

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

3条回答
  •  情深已故
    2021-01-26 11:47

    You can use also [[ instead of $

    myFunc2 <- function(x, y){
    +     z <- x[[y]]
    +     return(z)
    + }
    > myFunc2(BOD, 'demand')
    [1]  8.3 10.3 19.0 16.0 15.6 19.8
    

提交回复
热议问题