Reputation: 103
I have a data frame A with a column called "states". The states are recorded by their full name, ex. "California". There are multiple rows for each state.
I have a data frame B, which has the number of gun deaths for each state. The states are recorded by abbreviations, ex. "CA"
What I would like is: I want each row in A to have the number of gun deaths for the corresponding state. I was planning to use dplyr::inner_join() for this.
But of course, the problem is that the state names are different in the different data frames.
What is the best way to make the names match?
Upvotes: 0
Views: 4436
Reputation: 263441
If you have two vectors of the same length and want to construct a translation table just add the input vector of state names as the names
-attribute of the output vector of state abbreviationa, and then pass in the names as inputs to the "["-function:
names(state.abb) <- state.name
# both are in-built values in the `state`-item of default `datasets` package
?state # also See: ?Constants and ?data
dfrm$abbrev <- state.abb[dfrm$states]
Upvotes: 5