0

I have 2 data-frames, one with 1093 observations containing data from every state over a time-span of n days, and the other data frame has 50 observations of 2 variables: state name and state populations. My ultimate goal is to create a percent of state population variable in the data-frame with 1093 observations. So my plan was to add the state populations to the data frame with 1093 observations by just matching up the state names. I tried this chunk of code:

df.state$population <- c(which(pop.states$State==df.state$state, pop.states$X2018.Population))

However, this did not work because there is a difference in lengths so my question is how can I add the population variable to this data-set or is there a different way for me to find the per capita measure of the variables found in the data-frame with 1093 observations? Thank you

Sam Kh
  • 35
  • 3
  • Please make this question *reproducible*. This includes sample code you've attempted (including listing non-base R packages, and any errors/warnings received), sample *unambiguous* data (e.g., `dput(head(x))` or `data.frame(x=...,y=...)`), and intended output. Refs: https://stackoverflow.com/questions/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Mar 26 '20 at 16:56

2 Answers2

1

What you want to do is called a "left join". Try this:

library(tidyverse)

df <- df.state %>% left_join(pop.states, by = c("State" = "state"))

For more information and tutorials on these sort of approaches check R for data science.

Tim Draws
  • 71
  • 1
  • 7
0

For the base R way of doing it, you can use merge, for a left join like Tim's answer, you use an additional argument of all.x = TRUE

df.state <- data.frame(
  state = c("New York", "Maine"),
  observations = c(1, 2)
)

pop.states <- data.frame(
  State = c("New York", "Maine"),
  X2018.Population = c(100, 200)
)

merge(df.state, pop.states, by.x = "state", by.y = "State")
#>      state observations X2018.Population
#> 1    Maine            2              200
#> 2 New York            1              100