Ear*_*ien 3 r calculated-columns tidyverse
我想做这样的事情(一个愚蠢的代表):
iris %>%
mutate(mtcars = Sepal.Length * 7) %>%
filter(mtcars < max(mtcars$mpg))
Run Code Online (Sandbox Code Playgroud)
其中第一个mtcars引用 中的列iris,而第二个mtcars引用外部 data.frame。
我意识到我可以预先计算max(mtcars$mpg)或简单地重新标记 data.frame,但我想知道是否有一种方法可以在原位显式区分两者?例如:
iris %>%
mutate(mtcars = Sepal.Length * 7) %>%
filter(`mtcars` < max(EXTERNAL::mtcars$mpg))
# ^ ^
# column Not a column
Run Code Online (Sandbox Code Playgroud)
根据此链接 - https://rlang.r-lib.org/reference/dot-data.html,Tidyverse建议的方法是使用.data和.env代词来区分。
使用数据屏蔽函数进行编程时,.data 和 .env 代词明确了在何处查找对象。
所以在这种情况下,代码将是:
iris %>%
mutate(mtcars = Sepal.Length * 7) %>%
filter(mtcars < max(.env$mtcars$mpg))
Run Code Online (Sandbox Code Playgroud)
如果“其他”数据集不是包的一部分,或者您不一定先验知道该包,则可以get在 dplyr 管道外部的环境中使用。
e <- environment() # disposable after this next pipe
iris %>%
mutate(mtcars = Sepal.Length * 7) %>%
filter(mtcars < max(get("mtcars", envir = e)$mpg))
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species mtcars
# 1 4.7 3.2 1.3 0.2 setosa 32.9
# 2 4.6 3.1 1.5 0.2 setosa 32.2
# 3 4.6 3.4 1.4 0.3 setosa 32.2
# 4 4.4 2.9 1.4 0.2 setosa 30.8
# 5 4.8 3.4 1.6 0.2 setosa 33.6
# 6 4.8 3.0 1.4 0.1 setosa 33.6
# 7 4.3 3.0 1.1 0.1 setosa 30.1
# 8 4.6 3.6 1.0 0.2 setosa 32.2
# 9 4.8 3.4 1.9 0.2 setosa 33.6
# 10 4.7 3.2 1.6 0.2 setosa 32.9
# 11 4.8 3.1 1.6 0.2 setosa 33.6
# 12 4.4 3.0 1.3 0.2 setosa 30.8
# 13 4.5 2.3 1.3 0.3 setosa 31.5
# 14 4.4 3.2 1.3 0.2 setosa 30.8
# 15 4.8 3.0 1.4 0.3 setosa 33.6
# 16 4.6 3.2 1.4 0.2 setosa 32.2
Run Code Online (Sandbox Code Playgroud)
使用e <- environment()and then envir = e,我们允许以这种方式使用“本地”(可能是函数内)数据集,而不是假设或依赖全局环境数据集或包数据集等。
是的,在这种情况下您可以使用datasets::mtcars
iris %>%
mutate(mtcars = Sepal.Length * 7) %>%
filter(mtcars < max(datasets::mtcars$mpg))
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species mtcars
#> 1 4.7 3.2 1.3 0.2 setosa 32.9
#> 2 4.6 3.1 1.5 0.2 setosa 32.2
#> 3 4.6 3.4 1.4 0.3 setosa 32.2
#> 4 4.4 2.9 1.4 0.2 setosa 30.8
#> 5 4.8 3.4 1.6 0.2 setosa 33.6
#> 6 4.8 3.0 1.4 0.1 setosa 33.6
#> 7 4.3 3.0 1.1 0.1 setosa 30.1
#> 8 4.6 3.6 1.0 0.2 setosa 32.2
#> 9 4.8 3.4 1.9 0.2 setosa 33.6
#> 10 4.7 3.2 1.6 0.2 setosa 32.9
#> 11 4.8 3.1 1.6 0.2 setosa 33.6
#> 12 4.4 3.0 1.3 0.2 setosa 30.8
#> 13 4.5 2.3 1.3 0.3 setosa 31.5
#> 14 4.4 3.2 1.3 0.2 setosa 30.8
#> 15 4.8 3.0 1.4 0.3 setosa 33.6
#> 16 4.6 3.2 1.4 0.2 setosa 32.2
Run Code Online (Sandbox Code Playgroud)
更通用的解决方案是引用environment找到数据集的位置。例如,假设数据框位于您的全局环境中:
iris %>%
mutate(mtcars = Sepal.Length * 7) %>%
filter(mtcars < max(.GlobalEnv$mtcars$mpg))
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species mtcars
#> 1 4.7 3.2 1.3 0.2 setosa 32.9
#> 2 4.6 3.1 1.5 0.2 setosa 32.2
#> 3 4.6 3.4 1.4 0.3 setosa 32.2
#> 4 4.4 2.9 1.4 0.2 setosa 30.8
#> 5 4.8 3.4 1.6 0.2 setosa 33.6
#> 6 4.8 3.0 1.4 0.1 setosa 33.6
#> 7 4.3 3.0 1.1 0.1 setosa 30.1
#> 8 4.6 3.6 1.0 0.2 setosa 32.2
#> 9 4.8 3.4 1.9 0.2 setosa 33.6
#> 10 4.7 3.2 1.6 0.2 setosa 32.9
#> 11 4.8 3.1 1.6 0.2 setosa 33.6
#> 12 4.4 3.0 1.3 0.2 setosa 30.8
#> 13 4.5 2.3 1.3 0.3 setosa 31.5
#> 14 4.4 3.2 1.3 0.2 setosa 30.8
#> 15 4.8 3.0 1.4 0.3 setosa 33.6
#> 16 4.6 3.2 1.4 0.2 setosa 32.2
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
136 次 |
| 最近记录: |