【问题标题】:Replace NAs for a group of values with a non-NA character in group in R [duplicate]用R中的组中的非NA字符替换一组值的NA [重复]
【发布时间】:2021-12-12 22:44:01
【问题描述】:

如果此非 NA 字符并不总是出现在同一位置(第一行或其他),我正在尝试找到一种方法来逐组替换具有非 NA 字符的一组值的 NA。我发现的解决方案不适用于字符或仅基于先前或后续值填充。

这是一个数据示例:

participant_id <- c("ps1", "ps1", "ps1", "ps1", "ps2", "ps2", "ps3", "ps3", "ps3", "ps3")
test <- c("test1", NA, NA, NA, NA, "test2", NA, NA, "test3", NA)
data.frame(participant_id, test)

这就是我想要的结果:

participant_id test
ps1 test1
ps1 test1
ps1 test1
ps1 test1
ps2 test2
ps2 test2
ps3 test3
ps3 test3
ps3 test3
ps3 test3

【问题讨论】:

    标签: r fill


    【解决方案1】:

    这是使用来自zoo 包的na.locf 的另一种方法:

    library(zoo)
    library(dplyr)
    df %>% 
      group_by(participant_id) %>% 
      arrange(participant_id, test) %>% 
      mutate(test = zoo::na.locf(test, na.rm=FALSE))
    
       participant_id test 
       <chr>          <chr>
     1 ps1            test1
     2 ps1            test1
     3 ps1            test1
     4 ps1            test1
     5 ps2            test2
     6 ps2            test2
     7 ps3            test3
     8 ps3            test3
     9 ps3            test3
    10 ps3            test3
    

    【讨论】:

      【解决方案2】:

      我们可以在按“participant_id”分组后使用tidyr中的fill

      library(dplyr)
      library(tidyr)
      df1 <- df1 %>%
          group_by(participant_id) %>%
          fill(test, .direction = "downup") %>%
          ungroup
      

      -输出

      df1
      # A tibble: 10 × 2
         participant_id test 
         <chr>          <chr>
       1 ps1            test1
       2 ps1            test1
       3 ps1            test1
       4 ps1            test1
       5 ps2            test2
       6 ps2            test2
       7 ps3            test3
       8 ps3            test3
       9 ps3            test3
      10 ps3            test3
      

      数据

      df1 <- data.frame(participant_id, test)
      

      【讨论】:

        猜你喜欢
        • 2019-10-11
        • 2023-03-13
        • 1970-01-01
        • 2019-06-27
        • 2019-03-22
        • 2020-07-15
        • 2016-08-26
        • 2015-01-28
        相关资源
        最近更新 更多