【发布时间】:2020-04-24 02:10:02
【问题描述】:
我想在数据框中列出(或过滤)一组名称相似的列,为每一行评估这些列的值,然后使用结果创建一个新列。
现在我正在使用case_when,但我不知道提供一些通配符或已定义列的列表的方法。
我想列出一个列列表或创建一个过滤器,因为我想评估数据框中的几列,而不仅仅是少数几列。当列名彼此非常相似时,在case_when 中拥有一长串列似乎效率不高。
# Dummy data
ignore1 <- c(1, 0, 0)
ignore2 <- c(1, 0, 1)
col1 <- c(0, 1, 0)
col2 <- c(0, 1, 1)
col3 <- c(0, 1, 0)
df <- data.frame(ignore1, ignore2, col1, col2, col3)
df %>%
mutate(evaluation = case_when(
col1 == 0| col1 == 0 | col1 == 0 ~ "Failed",
TRUE ~ "Passed"
)
)
这是预期的结果:
ignore1 ignore2 col1 col2 col3 evaluation
1 1 1 0 0 0 Failed
2 0 0 1 1 1 Passed
3 0 1 0 1 0 Failed
第 2 行通过的位置,因为 col1、col2、col3 所有的值为 1。
【问题讨论】: