条件包括比较列内的结果元素

  • 本文关键字:结果 元素 包括 比较 条件 r
  • 更新时间 :
  • 英文 :


你能帮我完成以下任务吗?这是我数据集的模板:

Category <- c(1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3)
PrevRule <- c(-1, -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2)
UserRule <- c(2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 2, 0, 0, 0, 2, 2, 2, 2, 2, 2, 0, 0, 0, 1, 1, 1, 1)
Correct <- c(0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1)
df <- data.frame(Category, PrevRule, UserRule, Correct)

我需要根据以下规则创建一个额外的PP列:

  • i是行索引。 n是每个Category的最大行数。

  • 对于Category == 1
    如果Correct[i:i+2] == 0PP[i+2:n] = UserRule[i+2] 否则PP = NA

  • 对于Category > 1
    PP = PrevRule直到Correct[i:i+2] == 0
    然后PP[i+2:n] = UserRule[i+2]

因此,最后PP列应如下所示(以 NA 开头的列)

   Category PrevRule UserRule Correct PP
1         1       -1        2       0 NA
2         1       -1        2       0 NA
3         1       -1        2       0  2
4         1       -1        1       1  2
5         1       -1        1       1  2
6         1       -1        1       1  2
7         1       -1        1       1  2
8         1       -1        1       1  2
9         2        1        1       0  1
10        2        1        1       0  1
11        2        1        2       1  1
12        2        1        0       0  1
13        2        1        0       0  1
14        2        1        0       0  0
15        2        1        2       1  0
16        2        1        2       1  0
17        2        1        2       1  0
18        2        1        2       1  0
19        2        1        2       1  0
20        3        2        2       0  2
21        3        2        0       0  2
22        3        2        0       0  2
23        3        2        0       0  0
24        3        2        1       1  0
25        3        2        1       1  0
26        3        2        1       1  0
27        3        2        1       1  0

任何意见和建议将不胜感激!谢谢!

真的很难理解你的想法,但这是我的解决方案。它并不完美,因为它使用 while -Loop,但它应该可以工作:

ret <- array(dim = nrow(df))
i <- 1
while(i < nrow(df) - 3){
  if(Category[i] == 1 & all(Correct[i + 0:2] == 0)){
    tmp <- max(which(Category==1)) #end of category 1
    ret[(i + 2):tmp] <- UserRule[i+2]
    #set index i to the index-value of the last element in category 1
    i <- tmp
  }else{
    ret[i] <- NA #actual not necessary, because PP is NA per default.
  }
  print("From now on, I will only ask clear questions!")
  if(Category[i] >= 2){
    ret[i] <- PrevRule[i]
    if(all(Correct[(i-2):i] == 0)){#3 consecutive 0 in Correct
      tmp <-  max(which(Category == Category[i])) #end of current category
      ret[i:tmp] <- UserRule[i]
      i <- tmp #set index i to the index-value of the last element in the current category
    } 
  }
  i <- i + 1
}
df$PP <- ret 

最新更新