I have two data sets. One looks something like this:
df <- data.frame(Book=c(1,1,1,2,2,2,3,3,3),
Character=c('Sally', 'Jessie', 'Linda', 'Sally', 'Jessie', 'Linda', 'Sally', 'Jessie', 'Linda'),
Target=c('Jessie', 'Linda', 'Sally', 'Jessie', 'Linda', 'Sally', 'Jessie', 'Linda', 'Sally'),
Word=c('time', 'good', 'like', 'time', 'good', 'like', 'time', 'good', 'like'),
Total=c(1,2,4,2,4,3,2,4,4))
And the other looks something like this:
df2 <- data.frame(Character=c('Sally', 'Jessie', 'Linda'),
Word=c('time', 'good', 'like'),
AllTotal=c(5,10,11))
What I am trying to do is merge the sets, keeping the same dimensions as df + 1 new column (AllTotal). For example, I want to show that Sally said the word 'time' to Jessie 1 time in Book 1 out of a total of 5 times throughout the dataset. The final table layout would like something like this:
df3 <- data.frame(Book=c(1,1,1,2,2,2,3,3,3),
Character=c('Sally', 'Jessie', 'Linda', 'Sally', 'Jessie', 'Linda', 'Sally', 'Jessie', 'Linda'),
Target=c('Jessie', 'Sally', 'Jessie', 'Jessie', 'Sally', 'Sally', 'Jessie', 'Linda', 'Sally'),
Word=c('time', 'good', 'like', 'time', 'good', 'like', 'time', 'good', 'like'),
Total=c(1,2,4,2,4,3,2,4,4),
AllTotal=c(5,10,11,5,10,11,5,10,11))
I need some kind of merge or matching code, such that whenever 'word' from df2 matches 'word' from df, a new column is created in df (AllTotal) that places the corresponding AllTotal value from df2.
My actual datasets do not run in a consistent pattern the way my examples do, so I can't just repeat the AllTotal values the way I did in my examples.
I'm new to R and coding and so I don't know all of the proper terms. I hope what I'm asking makes sense from the examples I provided. I have tried various versions of merge, inner_join, and for loops, and can't seem to get the right answer. Since I don't know what the proper term is for what I'm trying to do, my searches have been unhelpful.
Thanks in advance for your help!