如何在R中进行FOR和IF循环,根据两个因子级别对值求和,并创建新变量?
我希望我的数据帧像如何在R中进行FOR和IF循环,根据两个因子级别对值求和,并创建新变量?,r,for-loop,if-statement,dplyr,R,For Loop,If Statement,Dplyr,我希望我的数据帧像 > trial_acc df$Condition df$id df$correct keep 1 1_1 006 1 no 2 3_3 006 1 no 3 1_3 006 5 no 4 3_1 006 3 no 5 1_1 0ui 13 yes 6
> trial_acc
df$Condition df$id df$correct keep
1 1_1 006 1 no
2 3_3 006 1 no
3 1_3 006 5 no
4 3_1 006 3 no
5 1_1 0ui 13 yes
6 3_3 0ui 18 yes
7 1_3 0ui 16 yes
8 3_1 0ui 13 yes
9 1_1 12f 12 no
10 3_3 12f 2 no
11 1_3 12f 13 no
12 3_1 12f 7 no
我需要创建一个变量“keep”,如果df$correct的总数大于等于40,则按df$id分组,否则为“no”
我试过了,但不起作用
for (i in levels(trial_acc$`df$id`)) {
if (trial_acc$`df$id` == i & sum(trial_acc$`df$correct`) >= 40){
trial_acc$keep <- "yes"
} else{
trial_acc$keep <- "no"
}
}
for(一级(试用版)$`df$id`){
如果(试用期会计科目$`df$id`==i&sum(试用期会计科目$`df$correct`)>=40){
trail\u acc$keepBytrail\u acc$keep如果您喜欢dplyr
,您可以这样做:(我使用@mt1022的数据和简化列)
trail\u acc%%>%group\u by(id)%%
变异(keep=ifelse(sum(correct)>=40,‘yes’、‘no’))%%
解组()
#一个tibble:12x4
条件id正确保持
1 1_1 006 1个
2 3_3 006 1个
3 1_3 006 5个
4 3_1 006 3号
5 1_1 0ui 13是
6 3_3 0ui 18是
7 130UI 16是
8 3_1 0ui 13是
9 1_1 12楼12号
10 3_3 12楼2号
11 1_3 12楼13号
12 3_1 12楼7号
trail_acc$keep <- 'no' # set default to "no"
for(i in unique(trail_acc$id)){
if(sum(trail_acc$correct[trail_acc$id == i]) >= 40){
trail_acc$keep[trail_acc$id == i] <- 'yes'
}
}
sum_by_id <- ave(trail_acc$correct, trail_acc$id, FUN = sum)
trail_acc$keep <- ifelse(sum_by_id >= 40, 'yes', 'no')
# Condition id correct keep
# 1 1_1 006 1 no
# 2 3_3 006 1 no
# 3 1_3 006 5 no
# 4 3_1 006 3 no
# 5 1_1 0ui 13 yes
# 6 3_3 0ui 18 yes
# 7 1_3 0ui 16 yes
# 8 3_1 0ui 13 yes
# 9 1_1 12f 12 no
# 10 3_3 12f 2 no
# 11 1_3 12f 13 no
# 12 3_1 12f 7 no
trail_acc <- structure(list(Condition = structure(c(1L, 4L, 2L, 3L, 1L, 4L,
2L, 3L, 1L, 4L, 2L, 3L), .Label = c("1_1", "1_3", "3_1", "3_3"
), class = "factor"), id = structure(c(1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 3L, 3L, 3L, 3L), .Label = c("006", "0ui", "12f"), class = "factor"),
correct = c(1L, 1L, 5L, 3L, 13L, 18L, 16L, 13L, 12L, 2L,
13L, 7L)), row.names = c("1", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "11", "12"), class = "data.frame")
trail_acc%>%group_by(id)%>%
mutate(keep = ifelse(sum(correct) >= 40, 'yes', 'no'))%>%
ungroup()
# A tibble: 12 x 4
Condition id correct keep
<fct> <fct> <int> <chr>
1 1_1 006 1 no
2 3_3 006 1 no
3 1_3 006 5 no
4 3_1 006 3 no
5 1_1 0ui 13 yes
6 3_3 0ui 18 yes
7 1_3 0ui 16 yes
8 3_1 0ui 13 yes
9 1_1 12f 12 no
10 3_3 12f 2 no
11 1_3 12f 13 no
12 3_1 12f 7 no