【问题标题】:filling in variable values using R使用 R 填充变量值
【发布时间】:2011-04-01 05:02:30
【问题描述】:

我有一个大型数据集,只需要填写一些缺失值。有没有办法使用 R 填充缺失值。这是一个示例数据集:

aid weight  birth_date  number_born
1   121 10/02/2009  14
2   111 10/02/2009  NA
3   132 NA           12
4   145 14/02/2009  11
5   221 NA           NA
6   131 25/02/2009  10
7   231 25/02/2009  NA

需要填写以下信息:

Aid = 3, birth_date = 13/02/2009
Aid = 5, birth_date = 17/02/2009
Aid = 2, number_born = 6
Aid = 5, number_born = 16
Aid = 7, number_born = 5 

我希望我的问题足够清楚,任何帮助将不胜感激。

波萨

【问题讨论】:

    标签: r variables


    【解决方案1】:

    如果data.frame df_with_missing 中有数据缺失值,data.frame fill_birth_date (fill_number_born) 中有要填写的数据。我假设辅助变量在 df_with_missing 中是唯一的。

    aid birth_date
    3 13/02/2009
    5 17/02/2009
    
    fill_birth_date$rec <- match(fill_birth_date$aid,df_with_missing$aid)
    df_with_missing$birth_date[fill_birth_date$rec] <- fill_birth_date$birth_date
    
    fill_number_born$rec <- match(fill_number_born$aid,df_with_missing$aid)
    df_with_missing$number_born[fill_number_born$rec] <- fill_number_born$number_born
    

    【讨论】:

    • 你应该强制(或读取)birth_date 作为字符(不是因素)。
    猜你喜欢
    • 1970-01-01
    • 2016-06-24
    • 2020-03-09
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多