我有两个这样的数据框:
#
df_1 <- data.frame(x = c('x4','x4','x5','x5','x5','x6','x6'),
y = c(0,0,1,1,1,0,0))
#
df_2 <- data.frame(x = c('x4','x4','x5','x5','x5','x7','x7'),
z = c(1,1,1,1,1,0,0))
我想基于列x合并它们,但是在新的df中只有每个df的x列中相同的行。示例输出:
x y z
x4 0 1
x4 0 1
x5 1 1
x5 1 1
x5 1 1
我试过这个
merge(x = df_1, y = df_2, by = "x", all = TRUE)
但没有成功。我该怎么办?
结果来自
merge(df_1, df_2)
x y z
1 x4 0 1
2 x4 0 1
3 x4 0 1
4 x4 0 1
5 x5 1 1
6 x5 1 1
7 x5 1 1
8 x5 1 1
9 x5 1 1
10 x5 1 1
11 x5 1 1
12 x5 1 1
13 x5 1 1
使用此:
intersect(df_1$x, df_2$x)
[1] "x4" "x5"
可以看到数据帧中的常见值。是否可以将它用作合并仅常见的行的规则?
答案 0 :(得分:1)
有了基础,正如jogo指出的那样,只需运行
merge(df_1, unique(df_2))
使用tidyverse,
library(tidyverse)
left_join(df_1, unique(df_2), by = "x")
x y z
1 x4 0 1
2 x4 0 1
3 x5 1 1
4 x5 1 1
5 x5 1 1