Using R to insert a value for missing data with a value from another data frame

无人久伴 提交于 2019-11-30 15:40:29

Using merge:

r = merge(Data, Donor, by="country", suffixes=c(".Data", ".Donor"))
Data$x = ifelse(is.na(r$x.Data), r$x.Donor, r$x.Data)

If for some reason idea of overwriting all values of x seems bad then use which to overwrite only NAs (with the same merge):

r = merge(Data, Donor, by="country", suffixes=c(".Data", ".Donor"))
na.idx = which(is.na(Data$x))
Data[na.idx,"x"] = r[na.idx,"x.Donor"]

Here's one option, should work generally:

#Get the vector of countries with missing x
country.na <- Data$country[is.na(Data$x)]
#Get corresponding location of x in Donor
index <- sapply(country.na, function(x) which(Donor$country == x))
#Replace NA values with corresponding values in Donor
Data$x[is.na(Data$x)] <- Donor$x[index]
Data
#    country year     x
# 1       70 1920 9.234
# 2       70 1921 9.234
# 3       70 1922 9.234
# 4       70 1923 9.234
# 5       70 1924 9.234
# 6       80 1920 1.523
# 7       80 1921 1.523
# 8       80 1922 1.523
# 9       80 1923 1.523
# 10      80 1924 1.523
# 11      90 1920 7.562
# 12      90 1921 7.562
# 13      90 1922 7.562
# 14      90 1923 7.562
# 15      90 1924 7.562
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!