我有这样一个数据框架:
ID <- c("A", "B", "C", "D", "E", "F")
Score1 <- c("(25-30)", "(31-40)", "(41-60)", "(25-30)","(25-30)","(25-30)")#(25-30) low problems cut-off
Score2 <- c("(0-5)", "(6-11)", "(25-30)", "(6-11)", "(0-5)", "(0-5)") #"(0-5)" low problems cut-off
Score3 <- c("(12-20)", "(21-42)", "(43-55)", "(12-20)", "(21-42)","(12-20)")#"(12-20)" low problems cut-off
Score4 <- c("(1-20)", "(21-60)", "(61-80)", "(1-20)", "(1-20)", "(1-20)")#"(1-20)" low problems cut-off
df <- data.frame(ID, Score1, Score2, Score3, Score4)我想要创建的小组,根据他们的类别,为1至4分。
这些得分类别是我的截止值低问题,中度问题,和高问题.
这个想法是,只要一个参与者跌入中等或高问题的类别之一,他们就会进入实验组,而那些得分较低的人就会进入控制组。
这就是为什么,我尝试了下面一位朋友的建议,但我的问题有点不同,我想这就是为什么它在用不同的逻辑。
下面,我想告诉R,把那些在所有分数中属于第一类的人放到对照组,而其他人则放在实验组。
df <- df %>%
mutate(Group = case_when(
Score1 == "(25-30)" | Score2 == "(0-5)" | Score3 == "(12-20)" | Score4 == "(1-20)"
~ "Control",
TRUE ~ "Experimental" ))但这是你最后得到的:
ID Score1 Score2 Score3 Score4 Group
1 A (25-30) (0-5) (12-20) (1-20) Control
2 B (31-40) (6-11) (21-42) (21-60) Experimental
3 C (41-60) (25-30) (43-55) (61-80) Experimental
4 D (25-30) (6-11) (12-20) (1-20) Control
5 E (25-30) (0-5) (21-42) (1-20) Control
6 F (25-30) (0-5) (12-20) (1-20) Control如您所见,参与者D和E在控制组中,虽然参与者D的Score2和参与者E的Score3在中等限值,换句话说,我在代码中没有指定的评分组。
只有当参与者没有处于所有分数的低问题极限时,他们才会被纳入实验组。我应该如何修改我的代码?
很抱歉我的问题太长了。非常感谢!
发布于 2022-11-22 20:39:17
如果所有分数都在较低的组中,即使用&和if_else,则更容易检查:
library(dplyr, warn = FALSE)
df |>
mutate(Group = if_else(Score1 == "(25-30)" & Score2 == "(0-5)" & Score3 == "(12-20)" & Score4 == "(1-20)", "Control", "Experimental"))
#> ID Score1 Score2 Score3 Score4 Group
#> 1 A (25-30) (0-5) (12-20) (1-20) Control
#> 2 B (31-40) (6-11) (21-42) (21-60) Experimental
#> 3 C (41-60) (25-30) (43-55) (61-80) Experimental
#> 4 D (25-30) (6-11) (12-20) (1-20) Experimental
#> 5 E (25-30) (0-5) (21-42) (1-20) Experimental
#> 6 F (25-30) (0-5) (12-20) (1-20) Controlhttps://stackoverflow.com/questions/74538627
复制相似问题