Passing a variable name to a function in R

前端 未结 4 855
悲&欢浪女
悲&欢浪女 2020-12-02 07:25

I\'ve noticed that quite a few packages allow you to pass symbol names that may not even be valid in the context where the function is called. I\'m wondering how this works

4条回答
  •  悲&欢浪女
    2020-12-02 07:55

    If you put the variable name between quotes when you call the function, it works:

    > b <- function(data,name) { within(data,print(name)) }
    > b(a, "x")
    [1] "x"
        x  y
    1   1  1
    2   2  2
    3   3  3
    4   4  4
    5   5  5
    6   6  6
    7   7  7
    8   8  8
    9   9  9
    10 10 10
    

提交回复
热议问题