yass2
yass2

Reputation: 27

Adding values from a dataframe to a different dataframe

I'm a noob in r programming. I have 2010 census data in the link- census data. This is my dataframe- dataframe.

What I'd like to do is add the population column 'P001001' from the census data for each state into the dataframe. I'm not able to figure out how to map the state abbreviations in the dataframe to the full names in the census data, and add the respective population to each row for that state in the data frame. The data is for all of the states. What should be the simplest way to do this?

Thanks in advance.

Upvotes: 0

Views: 89

Answers (1)

Peter
Peter

Reputation: 12739

Use the inbuilt datasets for USA states: state.abb and state.name see State name to abbreviation in R

Here's a simple bit of code which will give you a tidyverse approach to the problem.

1) add the state abbreviation to the census table

2) left join the census with the df by state abbrevation

library(tibble)
library(dplyr)

census <-tibble(name = c("Colorado", "Alaska"),
             poo1oo1 = c(100000, 200000))

census <- 
  census %>% 
  mutate(state_abb = state.abb[match(name, state.name)])

df <- tibble(date = c("2011-01-01", "2011-02-01"),
             state = rep("CO", 2),
             avg = c(123, 1234))

df <- 
  df %>% 
  left_join(census, by = c("state" = "state_abb"))

Upvotes: 2

Related Questions