所以,我有兩個數據集表示舊的和當前的地址。R - 合併並更新主數據集
> main
idspace id x y move
198 1238 33 4 stay
641 1236 36 12 move
1515 1237 30 28 move
> move
idspace id x y move
4 1236 4 1 move
我需要的是合併與舊(main
)新數據(move
)和更新main
一次合併。
我想知道是否可以在一個操作?
更新基於id
,這是個人標識符。
idspace
,x
,y
是位置ID。
所以,我需要輸出爲
> main
idspace id x y move
198 1238 33 4 stay
4 1236 4 1 move # this one is updated
1515 1237 30 28 move
我不知道我怎麼能做到這一點。
喜歡的東西
merge(main, move, by = c('id'), all = T, suffixes = c('old', 'new'))
然而,這是錯誤的,因爲我需要手工做那麼多操作。
任何解決方案?
數據
> dput(main)
structure(list(idspace = structure(c(2L, 3L, 1L), .Label = c("1515",
"198", "641"), class = "factor"), id = structure(c(3L, 1L, 2L
), .Label = c("1236", "1237", "1238"), class = "factor"), x = structure(c(2L,
3L, 1L), .Label = c("30", "33", "36"), class = "factor"), y = structure(c(3L,
1L, 2L), .Label = c("12", "28", "4"), class = "factor"), move = structure(c(2L,
1L, 1L), .Label = c("move", "stay"), class = "factor")), .Names = c("idspace",
"id", "x", "y", "move"), row.names = c(NA, -3L), class = "data.frame")
> dput(move)
structure(list(idspace = structure(1L, .Label = "4", class = "factor"),
id = structure(1L, .Label = "1236", class = "factor"), x = structure(1L, .Label = "4", class = "factor"),
y = structure(1L, .Label = "1", class = "factor"), move = structure(1L, .Label = "move", class = "factor")), .Names = c("idspace",
"id", "x", "y", "move"), row.names = c(NA, -1L), class = "data.frame")`
我認爲這是一個dup爲'tmp < - rbind(move,main); tmp [!duplicate(tmp $ id)],'邏輯工作得很好,假設這裏沒有其他要求。 – thelatemail
@thelatemail我正在考慮使用'sqldf',但我不知道這個API足夠好回答。 –
@TimBiegeleisen - 也許'sqldf(」 選擇COALESCE(b.idspace,a.idspace)作爲idspace, COALESCE(b.id,a.id)作爲ID, COALESCE(BX,AX)爲x, COALESCE (by,ay)as y, coalesce(b.move,a.move)as move from main a left join move b on a.id = b.id 「)' - 醜但它確實有效。 – thelatemail