0

I suspect this is an easy one. I have two datasets; dataset one has IDs in the first column, and there is exactly one row per ID. The second column has data (value1). The second dataset has the same IDs in column 1, but there are multiple instances of each ID. I would like to enter value1 into the corresponding rows of the second dataset, such that each ID gets the correct value1 from dataset1. So a one-to-many mapping problem.

Example datasets:

tC <- textConnection("ID    Cat1    Cat2
1   AA  BB
1   AA  BB
2   CC  DD
2   DD  EE
2   DD  FF
3   AA  GG")
data1 <- read.table(header=TRUE, tC)
close.connection(tC)
rm(tC)

tC <- textConnection("ID    Value1
1   a
2   b
3   c")
data2 <- read.table(header=TRUE, tC)
close.connection(tC)
rm(tC)

The final outcome should look like what results from:

tC <- textConnection("ID    Cat1    Cat2    value1
1   AA  BB  a
1   AA  BB  a
2   CC  DD  b
2   DD  EE  b
2   DD  FF  b
3   AA  GG  c")
data3 <- read.table(header=TRUE, tC)
close.connection(tC)
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
marcel
  • 389
  • 1
  • 8
  • 21

1 Answers1

1

The function merge is designed for combining information of multiple data frames.

merge(data1, data2)

#   ID Cat1 Cat2 Value1
# 1  1   AA   BB      a
# 2  1   AA   BB      a
# 3  2   CC   DD      b
# 4  2   DD   EE      b
# 5  2   DD   FF      b
# 6  3   AA   GG      c
Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168