I have the following dataframes:
db1 = data.frame(name = c('a', 'b', 'c', 'd'), age = c('10', '20', '30', '40'), tier = NA)
db2 = data.frame(name = c('a', 'a', 'c', 'b'), age = c('10', '10', '30', '20'), tier = c('1', '3', '4', '2'))
I want to enter the tier values from db2 into the same column in db1 if the name and age variables match.
I can do this with a for-loop but when we're dealing with thousands of rows this takes far too long. Is there a faster way to do this?
for (i in 1:nrow(db1)){
for (j in 1:nrow(db2)){
if (db1$name[i] == db2$name[j] & db1$age[i] == db2$age[j]){
db1$tier[i] = db2$tier[j]
}
}
}
tiercolumn fromdb1(db1$tier <- NULL). This would be a simplemerge:merge(db1, db2)Or to be specific -merge(db1, db2, by = c('name', 'age'))name=aandage=10? Take the first?