如何解决基于多个条件和字符串向量或tidyselect传递的相应变量名称,使用dplyr :: mutate创建新变量
我很确定之前已经讨论过这个问题,但是我正在努力说明这个问题: 例如,我正在寻找此数据框...
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的解决方案。到目前为止,我使用了方便的new()函数来变异多个变量。是否可以将其用于条件?这是另一个不起作用的示例:
iris %>%
mutate(has_petal_1.4 = across(c(starts_with('Petal')),function(x) {1.4 %in% x}))
我们非常感谢您的帮助。
解决方法
方法有多种,一种选择是sha1()
c_across
或者使用library(dplyr) # >= 1.0.0
iris %>%
rowwise %>%
mutate(has_petal_1.4 = any(c_across(varsel) == 1.4),width_greater_1 = all(c_across(ends_with('Width')) > 1)) %>%
ungroup
# A tibble: 150 x 7
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species has_petal_1.4 width_greater_1
# <dbl> <dbl> <dbl> <dbl> <fct> <lgl> <lgl>
# 1 5.1 3.5 1.4 0.2 setosa TRUE FALSE
# 2 4.9 3 1.4 0.2 setosa TRUE FALSE
# 3 4.7 3.2 1.3 0.2 setosa FALSE FALSE
# 4 4.6 3.1 1.5 0.2 setosa FALSE FALSE
# 5 5 3.6 1.4 0.2 setosa TRUE FALSE
# 6 5.4 3.9 1.7 0.4 setosa FALSE FALSE
# 7 4.6 3.4 1.4 0.3 setosa TRUE FALSE
# 8 5 3.4 1.5 0.2 setosa FALSE FALSE
# 9 4.4 2.9 1.4 0.2 setosa TRUE FALSE
#10 4.9 3.1 1.5 0.1 setosa FALSE FALSE
# … with 140 more rows
rowSums