0

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.

Community
  • 1
  • 1
yass2
  • 27
  • 5
  • 2
    Hi ! Could you please provide the data you are talking about ? Use the `dput` R function to transform your dataframe into a code string. As it is, we cant help you. – lrnv Apr 13 '20 at 15:59
  • @yass2, to add columns to a data frame we use generic method data$column_name <- c("state1","state2"). – Arun kumar mahesh Apr 13 '20 at 16:51
  • @Irnv I have provided the screen captures of the data frame and the census data in the post. Since this is my first post, I'm not able to add the pictures in the post – yass2 Apr 13 '20 at 17:59

1 Answers1

2

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"))

Peter
  • 11,500
  • 5
  • 21
  • 31