接下来的“特定”观察结果(NOCB)

时间:2018-05-07 16:11:06

标签: r dplyr locf

这是我的数据框:

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)

有没有办法做到这一点?

2 个答案:

答案 0 :(得分:3)

应用na.locf0后,检查NA的每个职位,如果现在是a,请将其重置为NA。如果您要覆盖status,请将第status2=行替换为status = if_else(is.na(status) & status2 == "a", NA_character_, status2), status2 = NULL) %>%

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      

答案 1 :(得分:1)

使用tidyr:fill的解决方案基于创建dummyStatus列。 fill dummyStatus使用.direction = "up"。现在,在验证以下值应为dummyStatus的检查后,使用此NA填充实际status列中的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)