我已經閱讀了很多類似於這個的問題,但沒有一個類似於我的答案。我很抱歉,如果這是多餘的,我只是看不到它。用另一個數據框填充NAs,兩個id變量
我有一個主數據集和一個備份數據集。當主用戶有NA時,我想查看備份,如果有與full.place.name和Year匹配的值,我想用該值替換NA。
primary
是
Year Firearm.Homicide Firearm.Suicide Firearm.Unintentional full.place.name
2010 0 <NA> 0 Adair County, KY
2010 10 19 <NA> Adams County, CO
backup
是
Year Firearm.Homicide Firearm.Suicide Firearm.Unintentional full.place.name
2010 NA 1 1 Adair County, KY
2010 NA NA 0 Adams County, CO
我要的是
Year Firearm.Homicide Firearm.Suicide Firearm.Unintentional full.place.name
2010 0 1 0 Adair County, KY
2010 10 19 0 Adams County, CO
我已經試過
library(data.table)
setDT(primary); setDT(backup)
primary[is.na(primary$Firearm.Homicide), primary$Firearm.Homicide := backup[backup, primary$Firearm.Homicide, on=c("Year", "full.place.name")]]
但是,最後添加了五列,並沒有得到任何正確的值。我也嘗試了ifelse語句和FillIn,我從來沒有接近過。這裏有五行數據:
primary<-structure(list(Year = c(2010, 2010, 2010, 2010, 2010),
Firearm.Homicide = c("0","10", "4", "3", NA), Firearm.Suicide = c(NA,"19", "5", "6",
NA), Firearm.Unintentional = c("0", NA, NA, "0", "0"), full.place.name = c("Adair County, KY",
"Adams County, CO", "Adams County, MS", "Adams County, PA", "Adams County, WI"
)), .Names = c("Year", "Firearm.Homicide", "Firearm.Suicide",
"Firearm.Unintentional", "full.place.name"), row.names = c(NA,
5L), class = "data.frame")
backup<-structure(list(Year = c(2010, 2010, 2010, 2010, 2010), Firearm.Homicide = c(NA,
NA, 4, 3, 3), Firearm.Suicide = c(1, NA, NA, NA, NA), Firearm.Unintentional = c(1,
0, 1, NA, NA), full.place.name = c("Adair County, KY", "Adams County, CO",
"Adams County, MS", "Adams County, PA", "Adams County, WI")), .Names = c("Year",
"Firearm.Homicide", "Firearm.Suicide", "Firearm.Unintentional",
"full.place.name"), row.names = c(NA, 5L), class = "data.frame")
我真的很感謝任何幫助!
他們不是互相映射現在,我怎麼能做到這一點? – user5457414
您可以首先按鍵列對兩個數據框進行排序,具體取決於它們是什麼,我猜這裏應該是「Year」和「full.place.name」? – Psidom