f <- function(x) enquo(x)
e <- f()
#<quosure: empty>
#~
Run Code Online (Sandbox Code Playgroud)
这些都不起作用:
> is_empty(e)
[1] FALSE
> is_missing(e)
[1] FALSE
> is_false(e)
[1] FALSE
> is_quosure(e)
[1] TRUE
Run Code Online (Sandbox Code Playgroud) 我希望能够以编程方式使用dplyr's case_when来替换基本R cut()函数.
目前,case_when可以通过NSE与外部参数一起使用,如:
library(dplyr)
library(rlang)
patterns <- list(
x <= 2 ~ "<=2",
x <= 4 ~ "2<->4",
x > 4 ~ ">4"
)
x <- 1:10
case_when(!!!patterns)
Run Code Online (Sandbox Code Playgroud)
我想要做的是:在变异中使用另一个变量
这个想法会是这样的,虽然我无法弄清楚如何让它工作:
library(dplyr)
patterns_lazy <- list(
!!quo(x) <= 2 ~ "<=2",
!!quo(x) <= 4 ~ "2<->4",
!!quo(x) > 4 ~ ">4"
)
x <- "cyl"
mtcars %>% mutate(ABC = case_when(!!!patterns_lazy))
Run Code Online (Sandbox Code Playgroud)
我希望能够定义我想要过滤的列(在字符串内),并检索这样的东西(这个例子不起作用,因为它是所需的语法):
x <- "cyl"
mtcars %>%
select(cyl) %>%
mutate(ABC = case_when(!!!patterns_lazy)) %>%
head()
cyl ABC
1 6 …Run Code Online (Sandbox Code Playgroud) 我一直在使用dplyr的quosures:
library(dplyr)
library(ggplot2)
thing <- quo(clarity)
diamonds %>% select(!!thing)
print(paste("looking at", thing))
Run Code Online (Sandbox Code Playgroud)
[1]"看着〜""看清晰度"
我真的想打印出放入现状的字符串值,但只能得到以下内容:
print(thing)
Run Code Online (Sandbox Code Playgroud)
<quosure:global>
〜清晰度
print(thing[2])
Run Code Online (Sandbox Code Playgroud)
明晰()
substr(thing[2],1, nchar(thing[2]))
Run Code Online (Sandbox Code Playgroud)
[1]"清晰度"
有没有更简单的方法来"取消引用"quo()?
一段时间以来,我一直试图了解 tidy eval 或如何在 tidyverse 中使用变量,但我似乎从未完全掌握它。
例如,我试图将 ggplot 与变量映射一起使用。这将是基本的 R 版本:
library(ggplot2)
var1 = "wt"
var2 = "mpg"
ggplot(mtcars, aes(x = get(var1), y = get(var2))) + geom_point()
Run Code Online (Sandbox Code Playgroud)
但是,根据我所看到的所有文档和讨论,“正确”的 quasiquotation 方式是:
ggplot(mtcars, aes(x = !!sym(var1), y = !!sym(var2))) + geom_point()
Run Code Online (Sandbox Code Playgroud)
也许这更类似于:
ggplot(mtcars, aes(x = !!as.symbol(var1), y = !!as.symbol(var2))) + geom_point()
Run Code Online (Sandbox Code Playgroud)
该get()方法对我来说更短且更具可读性。为什么 tidyverse 社区会回避它?
这是错误消息:
错误:loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) 中“ggplot2”的包或命名空间加载失败:命名空间“rlang”0.3.4 已经加载,但是>= 0.4.0 是必需的
除了警告消息:
包 'ggplot2' 是在 R 版本 3.6.1 下构建的
请帮忙。非常感谢。
我刚刚在tidyverse 风格指南{{ }}2.2.3 节中遇到了“拥抱运算符” 。
{{ }}R 中的拥抱运算符有什么作用?
我想要一个灵活的函数,summarize其中使用:
一个很好的例子是用户提供fun=weighted.mean()并指定权重参数w。
现在,我正在尝试使用.... 问题是我找不到一种方法来...引用数据框中的变量?下面的示例是使用 给出的across(),但如果我使用 ,也会发生同样的情况summarize_at()
谢谢!!
\nlibrary(tidyverse)\nfo1 <- function(df, fun=mean, ...){\n df %>% \n group_by(Species) %>% \n summarise(across(starts_with("sepal"), fun, ...))\n}\n\nfo1(iris)\n#> `summarise()` ungrouping output (override with `.groups` argument)\n#> # A tibble: 3 x 3\n#> Species Sepal.Length Sepal.Width\n#> <fct> <dbl> <dbl>\n#> 1 setosa 5.01 3.43\n#> 2 versicolor 5.94 2.77\n#> 3 virginica 6.59 2.97\nfo1(iris, fun=weighted.mean)\n#> `summarise()` ungrouping output (override …Run Code Online (Sandbox Code Playgroud) 我再次对如何实现这一目标感到困惑:
鉴于此数据框:
df <- tibble(
foo = c(1,0,1),
bar = c(1,1,1),
foobar = c(0,1,1)
)
Run Code Online (Sandbox Code Playgroud)
这个向量:
to_sum <- c("foo", "bar")
Run Code Online (Sandbox Code Playgroud)
我想获得列中值的行式总和to_sum。
期望的输出:
# A tibble: 3 x 4
# Rowwise:
foo bar foobar sum
<dbl> <dbl> <dbl> <dbl>
1 1 1 0 2
2 0 1 1 1
3 1 1 1 2
Run Code Online (Sandbox Code Playgroud)
输入它是有效的(显然)。
df %>% rowwise() %>%
mutate(
sum = sum(foo, bar)
)
Run Code Online (Sandbox Code Playgroud)
这不会:
df %>% rowwise() %>%
mutate(
sum = sum(to_sum)
)
Run Code Online (Sandbox Code Playgroud)
我理解,因为如果我要尝试:
df %>% rowwise() …Run Code Online (Sandbox Code Playgroud) 在这个问题的最终目标是构建以下未评估使用呼叫[R “的计算上的语言,在那里list,a_name并50L从参数提供。
list(a_name = 50L)
Run Code Online (Sandbox Code Playgroud)
内部看起来像
str(quote(list(a_name = 50L)))
# language list(a_name = 50L)
str(as.list(quote(list(a_name = 50L))))
#List of 2
# $ : symbol list
# $ a_name: int 50
Run Code Online (Sandbox Code Playgroud)
我将把我的变量放在一个列表中,这样进一步的代码就会更清晰。
params = list(my_fun = as.name("list"), my_name = "a_name", my_value = 50L)
# What I tried so far?
# 1. The first thing that one would try
substitute(my_fun(my_name = my_value),
params)
#list(my_name = 50L) ## `my_name` was not substituted!
# 2. …Run Code Online (Sandbox Code Playgroud)