【发布时间】:2020-12-01 13:23:54
【问题描述】:
我很确定之前已经讨论过这个问题,但我很难用语言表达这个问题: 比如我在找这个数据框...
iris %>%
mutate(has_petal_1.4 = Petal.Length == 1.4 | Petal.Width == 1.4,
width_greater_1 = Sepal.Width > 1 & Petal.Width > 1)
...无需明确命名条件中的变量。 有没有办法使用字符串向量传递变量名?不幸的是,这似乎不起作用:
varsel <- c('Petal.Length', 'Petal.Width')
iris %>%
mutate(has_petal_1.4 = 1.4 %in% c(!!! syms(varsel)))
此外,我想知道在 mutate() 函数中是否有使用 tidyselect 的解决方案。到目前为止,我使用了新的方便的 cross() 函数来改变多个变量。是否也可以在条件下使用它?这是另一个不起作用的示例:
iris %>%
mutate(has_petal_1.4 = across(c(starts_with('Petal')), function(x) {1.4 %in% x}))
非常感谢任何帮助。
【问题讨论】:
标签: r dplyr multiple-conditions