【问题标题】:Inpute missing value based on identifier [duplicate]根据标识符估算缺失值
【发布时间】:2021-05-31 06:03:06
【问题描述】:

我喜欢根据与匹配索引配对的值来填充某些变量的缺失值。示例(第一个 col 是索引;第二个 col 是值)。

mat <- cbind(c(1,1,2,2,3,3,4,4,4), c(4.3, NA, 2.4, NA, 3.8, NA, 1.2, NA, NA))

newmat <- cbind(c(1,1,2,2,3,3,4,4,4), c(4.3, 4.3, 2.4, 2.4, 3.8, 3.8, 1.2, 1.2, 1.2))

【问题讨论】:

    标签: r missing-data imputation


    【解决方案1】:

    我们可以使用fill

    library(dplyr)
    library(tidyr)
    mat %>% 
      as_tibble %>% 
      group_by(V1) %>% 
      fill(V2, .direction = 'updown')
    

    【讨论】:

    • 谢谢!!另外,为了确保它填充在上面和下面,我添加了 %>% 排列(V2)。否则,效果很好!
    • @DonZhang 你可以使用fill(V2, .direction = 'updown')
    • 哦,是的,谢谢!不知道!
    猜你喜欢
    • 2014-04-12
    • 2014-06-21
    • 2020-06-19
    • 2019-07-14
    • 1970-01-01
    • 1970-01-01
    • 2019-08-16
    • 2021-09-14
    相关资源
    最近更新 更多