0

Okay so I have two dataframes that look something like this:

df1 <- data.frame(name = c("ben", "sara", "claire", "tom", "tom", "glenn", "sara", "mary", "mary", "ben", "joe"), age = c(12, 19, 20, 14, 8, 34, 56, 4, 24, 16, 17))
df1
    name age
1     ben  12
2    sara  19
3  claire  20
4     tom  14
5     tom   8
6   glenn  34
7    sara  56
8    mary   4
9    mary  24
10    ben  16
11    joe  17

df2 <- data.frame(name = c("ben","alex", "tom", "steve", "sara", "mary", "mary", "ben", "joe"), age = c(16, 18, 20, 14, 8, 24, 45, 9, 24))
df2
 name age
1   ben  16
2  alex  18
3   tom  20
4 steve  14
5  sara   8
6  mary  24
7  mary  45
8   ben   9
9   joe  24

I want to pull out all the rows in df1 by name that have a name that also appear in df2. Something like this:

 names age
1   ben  12
2  sara  19
3   tom  14
4   tom   8
5  sara  56
6  mary   4
7  mary  24
8   ben  16
9   joe  17

How could I go about this?

To add some clarification: I don't want to join the two dataframes. I just want to pull the values from df1. I having trouble with this partly because there are multiple of some of the characters in both df1 and df2 and I am concerned with every occurrence of the characters in df1 regardless if they "line up" with df2.

  • You can use `intersect`: `df1[df1$name %in% intersect(df1$name, df2$name), ]` – Maël Oct 21 '22 at 07:42
  • When I run that I get an error message ```Error in intersect:df1[df1$name %in% intersect(df1$name, df2$name), ] : NA/NaN argument In addition: Warning message: In intersect:df1[df1$name %in% intersect(df1$name, df2$name), ] : numerical expression has 2 elements: only the first used``` – Ophelia Hanson Oct 21 '22 at 17:17

0 Answers0