missB
missB

Reputation: 59

merge table in R

I have the 2 tables as below

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)

I want to merge the two tables by gamble, only choose the gamble from table 1 which has the same number to gamble in table 2. The expected output is as follows:

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

Upvotes: 0

Views: 108

Answers (1)

Ricardo Saporta
Ricardo Saporta

Reputation: 55420

You are looking for merge

 merge(table1, table2, by.x=c("subj", "gamble"), by.y=c("subj2", "gamble2"), all=FALSE, sort=TRUE)

edited as per Ananda's helpful observation

Upvotes: 1

Related Questions