tchoup
tchoup

Reputation: 1023

left_join except for duplicate columns

I have two datafiles that have overlapping information. One includes estimates, and another includes precise information. I want to combine them into one dataframe, where I keep the estimates if there aren't exact counts, and otherwise replace the estimates with counts if available. How do I left join except where the column names are duplicated?

Dummy data:

#df 1
Id = c("1", "2", "3", "4")
Persons = c(300, 400, 200, 5000)
Houses = c(20, 40, 10, 23)
Ages =  c(45, 34, 50, 44)
Races = c(1, 3, 1, 2)

estimates = data.frame(Id, Persons, Houses, Ages, Races)

#df 2
Id = c("1", "2", "3", "4")
Persons = c(321, 421, 198, 4876)
Houses = c(21, 39, 11, 25)

counts = data.frame(Id, Persons, Houses)

Target dataframe:

  Id Persons Houses Ages Races
1  1     321     21   45     1
2  2     421     39   34     3
3  3     198     11   50     1
4  4    4876     25   44     2

Upvotes: 0

Views: 162

Answers (1)

akrun
akrun

Reputation: 887058

rows_update(estimates, counts)

-output

   Id Persons Houses Ages Races
1  1     321     21   45     1
2  2     421     39   34     3
3  3     198     11   50     1
4  4    4876     25   44     2

Upvotes: 2

Related Questions