使用R填充变量值

使用R填充变量值

问题描述:

我有一个很大的数据集,只需填写一些缺失的值。有没有一种方法,填补了使用R.这里缺失值的示例数据集:使用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 

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

Poasa

如果你有在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 
+0

@Sobala,无法让它工作.... – baz 2011-04-01 11:56:31

+0

你应该强制(或读取)birth_date作为字符(而不是因素)。 – 2011-04-01 13:57:40