我有两个数据框想要相互 append 。但是,如果 df1 中存在 ID 变量值,我只想 append df2。这是一种合并的 append ,但我不确定如何最好地做到这一点。数据看起来像这样
str(df1)
ID y x time
1 15 6 1
2 12 3 1
3 10 8 1
str(df2)
ID y x time
1 12 3 2
3 8 4 2
4 15 2 2
我想最终得到df3
:
ID y x time
1 15 6 1
2 12 3 1
3 10 8 1
1 12 3 2
3 8 4 2
intact_IL <- bind_rows(df1, df2)
给我两个 df1
中的每个人和df2
。各种尝试使用其他dplyr
动词对我不起作用。
感谢任何建议!
最佳答案
这个怎么样:
intact_IL <- bind_rows(df1, df2 %>% filter(ID %in% df1$ID))
关于r - 仅当 df1 中的行 ID 值也存在于 df2 中时才追加行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64013974/