I have two dataframes in R named house and candidates.
house
House Region Military_Strength
1 Stark The North 20000
2 Targaryen Slaver's Bay 110000
3 Lannister The Westerlands 60000
4 Baratheon The Stormlands 40000
5 Tyrell The Reach 30000
candidates
House Name Region
1 Lannister Jamie Lannister Westros
2 Stark Robb Stark North
3 Stark Arya Stark Westros
4 Lannister Cersi Lannister Westros
5 Targaryen Daenerys Targaryen Mereene
6 Baratheon Robert Baratheon Westros
7 Mormont Jorah Mormont Mereene
I want to merge the two dataframes on the basis of house. For that I have done:
merge(candidates, house, by="House", sort=FALSE)
The output is :
House Name Region.x Region.y Military_Strength
1 Lannister Jamie Lannister Westros The Westerlands 60000
2 Lannister Cersi Lannister Westros The Westerlands 60000
3 Stark Robb Stark North The North 20000
4 Stark Arya Stark Westros The North 20000
5 Targaryen Daenerys Targaryen Mereene Slaver's Bay 110000
6 Baratheon Robert Baratheon Westros The Stormlands 40000
I want to remove the second Name candidate from every house(if any), but its Military_Strength should be added up to the first candidate of the same house.
for eg:
4 Stark Arya Stark Westros The North 20000
would be removed but, 20000 would be added up to row3 Robb Stark Military_Strength. How to do it in appropriate way?