I have a dataframe a, with A, B, C are separate entries
Source Target N
A B 100
A D 200
I have another dataframe b for entries’ attributes
Name Rate1 Rate2
A 0.1 0.2
B 0.2 0.3
I want to calculate a new column Flow in a, as it is calculated row based by Flow = a$N * b[Name == a$Source]$Rate1. I tried to use apply by row, but I felt it’s slow. Is there a faster way?
Here’s a fairly expressive solution, fairly similar to the code you tried:
The
matchfunction is the basis formergeand%in%. It is particularly useful for constructing index vectors to pick from alternatives.