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.

3
  • 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. Commented Apr 13, 2020 at 15:59
  • @yass2, to add columns to a data frame we use generic method data$column_name <- c("state1","state2"). Commented Apr 13, 2020 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 Commented Apr 13, 2020 at 17:59

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. Thank you Peter!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.