(已更新)我的dataframe1看起来像:
A C D E F
1 c f x i e
2 d g i d d
3 e h p e e
和dataframe2:
A B
1 c q
2 d m
请注意,dataframe2 $ A是dataframe1 $ A的子集。如何基于dataframe2的A列对dataframe1进行子集设置,并将dataframe1的C列仅添加到dataframe2?我希望得到:
A B C
1 c q f
2 d m g
答案 0 :(得分:1)
您可以使用dplyr中的inner_join
:
library(dplyr)
df1 <- data.frame(A = c("c", "d", "e"),
C = c("f", "g", "h"))
df2 <- data.frame(A = c("c", "d"),
B = c("q", "m"))
inner_join(df1, df2)
#> Joining, by = "A"
#> Warning: Column `A` joining factors with different levels, coercing to
#> character vector
#> A C B
#> 1 c f q
#> 2 d g m
由reprex package(v0.3.0)于2020-04-08创建