I'm trying to add a column with the populations of each state in the US to data from the covdata package.
#remotes::install_github("kjhealy/covdata")
library(covdata)
# the csv has state names in one column (state_ab) and populations in a second column (pop_2019)
pops_ref <- read_csv("state_pops.csv") %>%
mutate(pop_2019 = as.character(pop_2019))
covus %>%
group_by(state, date) %>%
pivot_wider(names_from = measure, values_from = count) %>%
ungroup() %>%
mutate(state_pop = str_replace_all(state, pops_ref$state_ab, pops_ref$pop_2019))
I get the error of "longer object length is not a multiple of shorter object length" and the state_pop column shows identical values to the state column.
I know I could write a brutal case_when function, but since there are 50 states I would prefer to not do that. Is there any way to do this easier? I spent a few hours googling around for some simple way to do a definition or recode or replace and I have come up with nothing. What am I missing?
whoops! Of course, as soon as I finally wrote this question up and gave a last-ditch google effort, I found the solution.
the plyr function mapvalues is exactly what I needed.
I tried using a variety of joins, but frankly they scare me and I thought there had to be a better solution than joining a 50-row data frame to a ~81000 row data frame.