下一个"specific"观察向后进行(NOCB)

Next "specific" observation carried backward (NOCB)

这是我的数据框:

library(zoo)
library(dplyr)

df <- data.frame(
  id = rep(1:4, each = 4), 
  status = c(
    NA, "a", "c", "a", 
    NA, "c", "c", "c",
    NA, NA, "a", "c",
    NA, NA, "c", "c"),
  otherVar = letters[1:16],
  stringsAsFactors = FALSE)

对于变量状态,我希望下一个观察在组 (id) 内向后进行。

df %>% group_by(id) %>% na.locf(fromLast = TRUE) %>% ungroup

但是,我只希望我的 "c" 的倒退,而不是 "a" 的倒退。

来自变量状态:

NA "a" "c" "a" NA "c" "c" "c" NA NA "a" "c" NA NA "c" "c"

我想得到:

NA "a" "c" "a" "c" "c" "c" "c" NA NA "a" "c" "c" "c" "c" "c"

分别为:

data.frame(
  id = rep(1:4, each = 4), 
  status = c(
    NA, "a", "c", "a", 
    "c", "c", "c", "c",
    NA, NA, "a", "c",
    "c", "c", "c", "c"),
  otherVar = letters[1:16],
  stringsAsFactors = FALSE)

有办法吗?

使用 tidyr:fill 的解决方案基于创建 dummyStatus 列。 fill dummyStatus 使用 .direction = "up"。现在使用此 dummyStatus 在实际 status 列中填充 NA 值,然后验证检查以下值应为 c.

library(dplyr)
library(tidyr)
df %>% group_by(id) %>%
    mutate(dummyStatus = status) %>%
    fill(dummyStatus, .direction = "up" ) %>%
    mutate(status = ifelse(is.na(status) & lead(dummyStatus)=="c","c",status)) %>%
    select(-dummyStatus) %>% as.data.frame()

  #    id status otherVar
  # 1   1   <NA>        a
  # 2   1      a        b
  # 3   1      c        c
  # 4   1      a        d
  # 5   2      c        e
  # 6   2      c        f
  # 7   2      c        g
  # 8   2      c        h
  # 9   3   <NA>        i
  # 10  3   <NA>        j
  # 11  3      a        k
  # 12  3      c        l
  # 13  4      c        m
  # 14  4      c        n
  # 15  4      c        o
  # 16  4      c        p

数据:

df <- data.frame(
  id = rep(1:4, each = 4), 
  status = c(
    NA, "a", "c", "a", 
    NA, "c", "c", "c",
    NA, NA, "a", "c",
    NA, NA, "c", "c"),
  otherVar = letters[1:16],
  stringsAsFactors = FALSE)

应用 na.locf0 后,检查每个 NA 的位置,如果现在是 a,则将其重置回 NA。如果你想覆盖 status 然后用 status = if_else(is.na(status) & status2 == "a", NA_character_, status2), status2 = NULL) %>%

替换第二行 status2=
library(dplyr)
library(zoo)

df %>% 
  group_by(id) %>% 
  mutate(status2 = na.locf0(status, fromLast = TRUE),
         status2 = if_else(is.na(status) & status2 == "a", NA_character_, status2)) %>%
  ungroup

给予:

# A tibble: 16 x 4
      id status otherVar status2
   <int> <chr>  <chr>    <chr>  
 1     1 <NA>   a        <NA>   
 2     1 a      b        a      
 3     1 c      c        c      
 4     1 a      d        a      
 5     2 <NA>   e        c      
 6     2 c      f        c      
 7     2 c      g        c      
 8     2 c      h        c      
 9     3 <NA>   i        <NA>   
10     3 <NA>   j        <NA>   
11     3 a      k        a      
12     3 c      l        c      
13     4 <NA>   m        c      
14     4 <NA>   n        c      
15     4 c      o        c      
16     4 c      p        c