DoeyFoey DoeyFoey - 2 months ago 6
R Question

R filtering table and inserting information

I currently have following table structure

Table1

id a
11 4
11 3
22 1
22 3
22 5
33 2
33 1
44 6
44 8
66 5
66 7
77 6


Table2

id score
11 12
33 22
44 20


I want to delete every row from Table1, that does not contain any of the id in Table2$id.
unique(Table2$id)
should generate such an unique id list. Further, I need to write the score from Table2 into each corresponding id of Table1. The desired dataframe would be:

Result

id a score
11 4 12
11 3 12
33 2 22
33 1 22
44 6 20
44 8 20

Answer
merge(Table2, Table1, by = "id")

should do it.

Comments