user9895243
user9895243

Reputation: 49

Comparing two data frames according to several columns simultaneously in R

I have 2 data frames:

df1
Syllable Duration
Bis      0.18
Zeks     0.34
Ben      0.11

df2
Syllable Duration Pitch
Bis      0.18     78
Zeks     0.34     67
Bs       0.19     34
Ben      0.11     69

And I need to get a new data frame like this:

df3
Syllable Duration Pitch
Bis      0.18      78
Zeks     0.34      67
Ben      0.11      69

I've tried many things, but nothing gets me what I want. Any help would be of value to me.

This is one of the things I tried:

df1$Pitch <- df1$Pitch[match(df2$Syllable[df2$Duration],df1$Syllable[df1$Duration])]

Upvotes: 1

Views: 46

Answers (1)

dgr379
dgr379

Reputation: 345

The comments are right. But if you have duplicates in your data, it can cause multiplications of rows in your outcome. Use this to get rid of duplicates:

df3 <- merge(df2, df1[!duplicated(df1$Syllable),], by="Syllable")

Upvotes: 1

Related Questions