变异/过滤与另一个数据框同名的列

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)

the*_*ail 9

根据此链接 - 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)

  • 我相信 `.env` 的行为与我使用 `get(.., envir=e)` 类似:它立即在 dplyr/mutate/&lt;verbfun&gt; 环境之外查找调用环境并尝试在那里解析(并且,就像 R 一样,在搜索树中进一步搜索)。例如,如果您的 iris %&gt;% ... 位于函数内部,并且您从全局调用该函数,则 .env$.. 将首先查看您的函数(在 mutate 之外),然后在全局中,在第一场比赛时停止。 (2认同)

r2e*_*ans 8

如果“其他”数据集不是包的一部分,或者您不一定先验知道该包,则可以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,我们允许以这种方式使用“本地”(可能是函数内)数据集,而不是假设或依赖全局环境数据集或包数据集等。


All*_*ron 5

是的,在这种情况下您可以使用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)

  • 就像你写@GuedesBF那样做!尽管我们必须首先导入到全球环境中。 (2认同)
  • @r2evans 不,我认为你的答案是不同的 - 它更笼统。可能在“get”中设置“pos = 2”而不是提供“env”会阻止您预先定义一个新环境? (2认同)