最后观察以价值和列为条件进行

时间:2019-11-26 18:25:36

标签: r imputation locf

我有一个纵向的数据框,其中有很多看起来像这样的缺失值。

ID = c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3)
date = c(1,2,3,4,5,1,2,3,4,5,1,2,3,4,5)
cond = c(0,0,0,1,0,0,0,0,1,0,0,0,0,0,0)
var = c(1, NA , 2, 0,NA, NA, 3, NA,0, NA, 2, NA, 1,NA,NA)
df = data.frame(ID, date, cond,var)

我想根据两个条件来进行最后的观察:

1)在cond=0时应进行观察,关注变量的较高值。

2)当cond=1时,应结转所关注变量的较低值。

有人知道我如何以一种优雅的方式做到这一点吗?

最终数据集应如下图所示

ID = c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3)
date = c(1,2,3,4,5,1,2,3,4,5,1,2,3,4,5)
cond = c(0,0,0,1,0,0,0,0,1,0,0,0,0,0,0)
var = c(1, 1 , 2, 0, 0, NA, 3, 3, 0, 0,2,2,2,2,2)
final = data.frame(ID, date, cond,var)

到目前为止,我能够进行最后的观察,但是我无法施加条件

library(zoo)
df <- df %>%
  group_by(ID) %>% 
  mutate(var = 
           na.locf(var, na.rm = F))

欢迎任何建议

2 个答案:

答案 0 :(得分:2)

这是accumulate2的使用,即

df%>%
   group_by(ID)%>%
   mutate(d = unlist(accumulate2(var,cond[-1],function(z,x,y) if(y) min(z,x,na.rm=TRUE) else max(z,x,na.rm=TRUE))))
# A tibble: 15 x 5
# Groups:   ID [3]
      ID  date  cond   var     d
   <dbl> <dbl> <dbl> <dbl> <dbl>
 1     1     1     0     1     1
 2     1     2     0    NA     1
 3     1     3     0     2     2
 4     1     4     1     0     0
 5     1     5     0    NA     0
 6     2     1     0    NA    NA
 7     2     2     0     3     3
 8     2     3     0    NA     3
 9     2     4     1     0     0
10     2     5     0    NA     0
11     3     1     0     2     2
12     3     2     0    NA     2
13     3     3     0     1     2
14     3     4     0    NA     2
15     3     5     0    NA     2

答案 1 :(得分:0)

我想,如果我明白你的追求,那是什么?

ID = c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3)
date = c(1,2,3,4,5,1,2,3,4,5,1,2,3,4,5)
cond = c(0,0,0,1,0,0,0,0,1,0,0,0,0,0,0)
var = c(1, NA , 2, 0,NA, NA, 3, NA,0, NA, 2, NA, 1,NA,NA)
df = data.frame(ID, date, cond,var)

使用case_when可以进行一些条件检查。我不确定您是否要返回所有“ ID”字段的最小值,但这将查看条件,然后滞后或导致找到一个不丢失的值

library(dplyr)
df %>% 
  mutate(var_imput = case_when(
    cond == 0 & is.na(var)~lag(x = var, n = 1, default = NA),
    cond == 1 & is.na(var)~lead(x = var, n = 1, default = NA),
    TRUE~var
  ))

哪种产量:

   ID date cond var var_imput
1   1    1    0   1         1
2   1    2    0  NA         1
3   1    3    0   2         2
4   1    4    1   0         0
5   1    5    0  NA         0
6   2    1    0  NA        NA
7   2    2    0   3         3
8   2    3    0  NA         3
9   2    4    1   0         0
10  2    5    0  NA         0
11  3    1    0   2         2
12  3    2    0  NA         2
13  3    3    0   1         1
14  3    4    0  NA         1
15  3    5    0  NA        NA

如果要按ID分组,则可以按ID生成估算表,然后将其与原始表连接,如下所示:

# enerate input table
input_table <- df %>% 
  group_by(ID) %>% 
  summarise(min = min(var, na.rm = T),
            max = max(var, na.rm = T)) %>% 
  gather(cond, value, -ID) %>% 
  mutate(cond = ifelse(cond == "min", 0, 1))

# Join and impute missing
df %>% 
  left_join(input_table,by = c("ID", "cond")) %>% 
  mutate(var_imput = ifelse(is.na(var), value, var))