我有兩個資料集,一個用于從其他縣migration inflow到A縣,另一個用于migration outflow從縣A到其他縣。為了將兩個資料集組合為:
County Inflow Outflow
兩個資料集之間的公共列是Origin_Placeinmigration inflow和Dest_placein migration outflow。部分問題在于,Origin_Place并且Dest_place行數不相等。
如何將兩者組合成一個資料集,這樣我就不必對每個常見的縣名進行硬編碼?
我的原始遷移流出資料有 517 個觀測值,遷移流入資料有 441 個,因此每個資料集中的縣數不同。
虛擬資料:
# People moving out of county A to other counties
Origin_County_Name = c("A", "A", "A", "A", "A", "A", "A")
Individuals = c(223, 224, 2333, 4444, 5555, 6666, 7777)
Dest_place = c("B", "C", "D", "E", "F", "G", "H")
Outflow_df = data.frame(Origin_County_Name, Individuals, Dest_place)
# People moving in county A from other counties
Origin_Place = c("D", "E", "F")
Individuals = c(111, 8888, 9999)
Dest_County_Name = c("A", "A", "A")
Inflow_df = data.frame(Origin_Place, Individuals, Dest_County_Name)
uj5u.com熱心網友回復:
我想這就是你所追求的?
names(Outflow_df)[names(Outflow_df) == "Dest_place"] <- "County"
names(Outflow_df)[names(Outflow_df) == "Individuals"] <- "Outflow"
names(Inflow_df)[names(Inflow_df) == "Origin_Place"] <- "County"
names(Inflow_df)[names(Inflow_df) == "Individuals"] <- "Inflow"
merge(Outflow_df[, c("County", "Outflow")], Inflow_df[, c("County", "Inflow")], all = TRUE)
uj5u.com熱心網友回復:
您需要進行外部合并。使用all=Tmerge() 函式的引數
dfNew <- merge(Outflow_df,Inflow_df,by.x='Dest_place',by.y='Origin_Place',all=TRUE)
dfNew <- dfNew[,c('Dest_place','Individuals.y','Individuals.x')]
rename(dfNew,c('County','Inflow','Outflow'))
dfNew[is.na(dfNew)] <- ''
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/521632.html
標籤:rdplyr
