Rather than doing a nested for loop like this:
for (rowAll in 1:nrow(groupDataUnadjusted)) {
year <- groupDataUnadjusted[rowAll, "year"]
income <- groupDataUnadjusted[rowAll, "income_group"]
joint <- groupDataUnadjusted[rowAll, "Joint"]
child <- groupDataUnadjusted[rowAll, "children"]
for (rowPuf in 1:nrow(nationalPuf)) {
yearPuf <- nationalPuf[rowPuf, "year"]
incomePuf <- nationalPuf[rowPuf, "income_group"]
jointPuf <- nationalPuf[rowPuf, "Joint"]
childPuf <- nationalPuf[rowPuf, "children"]
if ((year == yearPuf) && (income == incomePuf) && (joint == jointPuf) && (child == childPuf)) {
groupDataUnadjusted[rowAll, 'tax_difference_pct'] <- groupDataUnadjusted[rowAll, 'tax_difference_pct'] + nationalPuf[rowPuf, 'diff']
break
}
}
}
groupDataAdjusted <- groupDataUnadjusted
I feel like there must be a faster way to find the corresponding rows between two dataframes. I am matching by taking to dataframes, different lengths, and looking where three columns are the same. If they are the same, I know that row is a match between them. Then I take one value from that row and add it to a value in the other dataframe.
But there must be a better way in R.