条件包括列

时间:2016-06-07 13:34:22

标签: 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

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

1 个答案:

答案 0 :(得分:0)

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

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