Let’s say you have a target list of words you want to yoke ratings from another dataframe. Maybe those values represent word frequency, concreteness, etc.
Here we start with a list of names. We eventually want to link IQ to each person’s name. That information is listed in another dataframe.
targets <- data.frame(words = c("mary", "linda", "sue"))
print(targets)
## words
## 1 mary
## 2 linda
## 3 sue
We have some database that lists IQ next to a series of names. The names in this dataframe are in a different order than the original list, and the dataframe contains additional names we don’t need data on.
lookup <- data.frame(words = c("john", "linda", "debbie", "melinda", "mary", "julie",
"sue"), iq = c(80, 90, 100, 110, 120, 130, 140))
print(lookup)
## words iq
## 1 john 80
## 2 linda 90
## 3 debbie 100
## 4 melinda 110
## 5 mary 120
## 6 julie 130
## 7 sue 140
Now we need to create a new dataframe outputting values for ‘iq’ for mary, linda, and sue. For this, we will use a left outer join. This creates a new dataframe that merges two dataframes only retaining rows that are specified in the first dataframe you enter (order matters!).
yoked <- merge(targets, lookup, by = "words", all.x = T) #words is the common or key variable, all.x specifies that this is a full outer join
print(yoked)
## words iq
## 1 linda 90
## 2 mary 120
## 3 sue 140
praise()
## [1] "You are phenomenal!"