I have two dataframes like this:
#
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))
I would like to merge them based on column x but in the new df have only the rows which are the same in both x column of every df. Example output:
x y z
x4 0 1
x4 0 1
x5 1 1
x5 1 1
x5 1 1
I tried this
merge(x = df_1, y = df_2, by = "x", all = TRUE)
but doesn't make. What can I do?
results from
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
Using this:
intersect(df_1$x, df_2$x)
[1] "x4" "x5"
it is possible to see which are the common values in the dataframes. Is it possible to use it as the rule to merge the rows which are only common?
df_1with "x4" is crossed with each row fromdf_2with "x4". So you will get 2*2=4 rows in the result. So please define the logic to reduce the result!cbind(df_1[df_1$x %in% df_2$x,], z=df_2[df_2$x %in% df_1$x, "z"])?