使用r中的条件语句将NA分配给行 [英] Assigning NAs to rows with conditional statement in r

查看:58
本文介绍了使用r中的条件语句将NA分配给行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下条件语句将NA分配给每个事件的前两行:如果每个事件的第一天的值都为"variable" = 0,请检查前一天.如果前一天(上一个事件的最后一天)的变量"> 0,则将NA分配给事件的前两行,且第一天的变量" = 0.如果前一天的变量" = 0,则不执行任何操作.

I'm trying to assign NAs to the first two rows of each event, with the following conditional statement: If the first day of each event has a value of "variable" = 0, check the day before. If the day before (last day of previous event) has a "variable" > 0, then assign NAs to the first two rows of the event having "variable" = 0 on the first day. If the day before has a "variable" = 0, do nothing.

这里是一个例子:

day <- c(1:16)
event<- c(1,1,2,3,4,4,4,5,5,5,6,6,6,7,7,7)
variable<- c(0,0,5,0,0,0,10,0,1,1,0,0,0,0,0,0)
A<- data.frame(day, event, variable)

     day  event  variable
1     1     1        0
2     2     1        0
3     3     2        5
4     4     3        0
5     5     4        0
6     6     4        0
7     7     4       10
8     8     5        0
9     9     5        1
10   10     5        1
11   11     6        0
12   12     6        0
13   13     6        0
14   14     7        0
15   15     7        0
16   16     7        0

以及它的外观

     day  event  variable
1     1     1        0
2     2     1        0
3     3     2        5
4     4     3       NA
5     5     4        0
6     6     4        0
7     7     4       10
8     8     5       NA
9     9     5       NA
10   10     5        1
11   11     6       NA
12   12     6       NA
13   13     6        0
14   14     7        0
15   15     7        0
16   16     7        0

注意:是否必须为事件1分配NA并不重要我尝试使用if条件执行此操作,但效果不佳.任何想法?并预先感谢!

Note: It doesn't matter if event 1 has to be assigned with NAs I tried to do this with if conditions, but is not working well. Any idea? and thanks in advance!

推荐答案

来自OP的新示例数据

library(data.table)
event2<- c(1,2,2,3,4,4,4,4,4,5,5) 
variable2<- c(140, 0, 69, 569, 28, 0,0,0,100,0,0) 
desire_output<- c(140, NA, NA, 569, 28, 0,0,0,100, NA,NA) 
A2<- data.frame(event2, variable2, desire_output) 

setDT(A2)

A2[,first_days_event:=fifelse(.I==min(.I),1,fifelse(.I==min(.I)+1,2,NA_integer_)),by=.(event2)]

A2[,result:={v <- variable2
for (i in 2:.N) {
  if (is.na(first_days_event[i])) {
    v[i] <- variable2[i]
  } else if (first_days_event[i]==1 & variable2[i]==0){
    if (variable2[i-1]>0) {
      v[i] <- NA_integer_
      if (first_days_event[i+1]==2) {
        v[i+1] <- NA_integer_
      }
    }
  }
}
v}]
A2
#>     event2 variable2 desire_output first_days_event result
#>  1:      1       140           140                1    140
#>  2:      2         0            NA                1     NA
#>  3:      2        69            NA                2     NA
#>  4:      3       569           569                1    569
#>  5:      4        28            28                1     28
#>  6:      4         0             0                2      0
#>  7:      4         0             0               NA      0
#>  8:      4         0             0               NA      0
#>  9:      4       100           100               NA    100
#> 10:      5         0            NA                1     NA
#> 11:      5         0            NA                2     NA


我将使用这个简单的 loop 解决方案.只需创建一个标志来指示每个事件的前两天.


I will use this simple loop solution. Just need to create a flag indicating the first tow days of each event.

library(data.table)

day <- c(1:16)
event<- c(1,1,2,3,4,4,4,5,5,5,6,6,6,7,7,7)
variable<- c(0,0,5,0,0,0,10,0,1,1,0,0,0,0,0,0)
A<- data.frame(day, event, variable)

setDT(A)


A[,first_days_event:=fifelse(.I==min(.I),1,fifelse(.I==min(.I)+1,2,NA_integer_)),by=.(event)]

A[,result:={v <- numeric(.N)
  for (i in 2:.N) {
    if (is.na(first_days_event[i])) {
      v[i] <- variable[i]
    } else if (first_days_event[i]==1){
      if (variable[i-1]>0) {
        v[i] <- NA_integer_
        if (first_days_event[i+1]==2) {
          v[i+1] <- NA_integer_
        }
      } else {
        v[i] <- variable[i]
      }
    }
  }
v}]

A
#>     day event variable first_days_event result
#>  1:   1     1        0                1      0
#>  2:   2     1        0                2      0
#>  3:   3     2        5                1      5
#>  4:   4     3        0                1     NA
#>  5:   5     4        0                1      0
#>  6:   6     4        0                2      0
#>  7:   7     4       10               NA     10
#>  8:   8     5        0                1     NA
#>  9:   9     5        1                2     NA
#> 10:  10     5        1               NA      1
#> 11:  11     6        0                1     NA
#> 12:  12     6        0                2     NA
#> 13:  13     6        0               NA      0
#> 14:  14     7        0                1      0
#> 15:  15     7        0                2      0
#> 16:  16     7        0               NA      0

这篇关于使用r中的条件语句将NA分配给行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆