我有一个包含许多列的数据集,通过每个行值组合,为另一列中的新值确定一组规则。不同的组合是多样的,并非所有列都包含在每个规则中。此外,一些列的微生物名称往往很长。因此,我使用的当前方法(case_when
)变得相当凌乱,回顾这些规则变得相当乏味。
我想知道是否有一个更好的方法来做到这一点,更干净,更容易审查?我运行这个数据集有超过70.000观察,所以下面是一个虚拟数据集,可以使用。
col1 col2 col3 col4 col5 col6
1 A 43 string1 AA verylongnamehere
2 B 22 string2 BB anotherlongname
3 C 15 string3 CC yetanotherlongname
4 D 100 string4 DD hereisanotherlongname
5 E 60 string5 EE thisisthelastlongname
test <- data.frame(
col1 = c(1,2,3,4,5),
col2 = c("A","B","C","D","E"),
col3 = c(43,22,15,100,60),
col4 = c("string1","string2","string3","string4","string5"),
col5 = c("AA","BB","CC","DD","EE"),
col6 = c("verylongnamehere", "anotherlongname","yetanotherlongname","hereisanotherlongname","thisisthelastlongname")
)
下面的代码是我使用的规则和代码的一个示例:
library(dplyr)
test2 <- test %>%
mutate(new_col = case_when(
col1 == 1 & col2 == "A" & col6 == "verylongnamehere" ~ "result1",
col3 >= 60 & col5 == "DD" ~ "result2",
col1 %in% c(2,3,4) &
col2 %in% c("B","D") &
col5 %in% c("BB","CC","DD") &
col6 %in% c("anotherlongname","yetanotherlongname") ~ "result3",
TRUE ~ "result4"
))
1条答案
按热度按时间8yparm6h1#
如果条件是在电子表格中,那么查看它们可能会更容易。下面是如何从电子表格中读取条件并构建
case_when
。电子表格表示(conditions.xlsx):
请注意,
==
和%in%
被视为默认值,此处未明确包含。加载条件
dput(cond)
:将条件处理为
case_when
命令:case.when
是字符串形式的case_when
命令:现在我们只需要解析、求值并在
mutate
中使用:根据您的示例,我只考虑了使用
&
作为逻辑运算符的条件,如果还使用|
,则必须在电子表格中为指定逻辑运算符的每个数据列添加另一列(&
或|
)。如果条件更复杂,并且带有括号,则可能无法使用此方法。