我有
df <- data.frame(
var1 = c(0, 1, 0, 1, 1, 0, 1),
var2 = c("1", "1", "0", "0", "1", "1", "0"),
var3 = c("1", "1", "0", "0", "1", "3", "1"),
var4 = c(0, 0, 1, 1, 1, 0, 3),
var5 = c("Yes", "No", "Yes", "No", "Yes", "No", "Yes"),
var6 = c("Yes", "No", "Yes", "No", "Yes", "No", "Maybe")
)
我想搜索具有严格和仅0
/1
值的所有变量,并将它们转换为具有"No"
/"Yes"
标签的因子。
我的尝试是
df %>%
mutate(across(where(unique(.) == c("0", "1")),
.fns = ~factor(ifelse(., "Yes", "No"), levels = c("Yes", "No"))))
但似乎不起作用。
Error in `mutate()`:
! Problem while computing `..1 = across(...)`.
Caused by error in `across()`:
! Problem while evaluating `where(unique(.) == c("0", "1"))`.
Caused by error in `where()`:
! Can't convert `fn`, a logical matrix, to a function.
我在tidyverse
/dplyr
pipe
系统中寻找解决方案。我不想指定要搜索或转换的变量。
在上面的示例中,只应转换var1
和var2
。
1条答案
按热度按时间k4ymrczo1#
您应该能够做到: