【发布时间】:2020-12-04 00:52:43
【问题描述】:
> counties %>% select(state, county, population, poverty)
> # also written as
> select(counties , state, county, population, poverty)
> state
Error: object 'state' not found
大家好,
我有一个关于这里的 select 函数到底传递了什么的问题,state、county、population 和 poverty 实际上不是绑定到封闭环境的变量,而是第一个的列名元素。这使得传递给函数的参数实际上是有状态的。
通常,在其他语言中,这些键将作为字符串传入,所以我只是想知道我们应该如何推理和考虑这些未绑定的变量!或许另外,R 解释器/解析器如何在后台处理这个问题。
【问题讨论】:
-
在data.frame的引用中,这些列名引用变量。如果您将 data.frame 附加到本地环境,您将能够直接引用它们。只要附上
attach(counties),你就可以在本地环境中使用state作为变量名。 -
一般不建议使用
attach(),因为它可能会导致各种错误,主要是因为它会导致混淆命名空间。
标签: r dplyr arguments interpreter