R中的合并表

时间:2013-07-02 18:07:23

标签: r merge dataframe

我有2个表格如下

subj <- c(1, 1, 1, 2, 2, 2, 3, 3, 3)
gamble <- c(1, 2, 3, 1, 2, 3, 1, 2, 3)
ev <- c(4, 5, 6, 4, 5, 6, 4, 5, 6)
table1 <- data.frame(subj, gamble, ev)

subj2 <- c(1, 2, 3)
gamble2 <- c(1, 3, 2)
table2 <- data.frame(subj2, gamble2)

我想通过赌博合并两个表,只选择表1中与赌博相同数字的赌博。预期输出如下:

 sub gamble ev
  1      1  4
  2      3  6
  3      2  5

1 个答案:

答案 0 :(得分:1)

您正在寻找merge

 merge(table1, table2, by.x=c("subj", "gamble"), by.y=c("subj2", "gamble2"), all=FALSE, sort=TRUE)
根据Ananda有用的观察编辑