Skip to content

Instantly share code, notes, and snippets.

@mattspence
Created February 21, 2018 22:06
Show Gist options
  • Save mattspence/a54a8733e5d75675b4abf527471a7614 to your computer and use it in GitHub Desktop.
Save mattspence/a54a8733e5d75675b4abf527471a7614 to your computer and use it in GitHub Desktop.
Count First, Second, and Third+ Generations from 2015, 2016, and 2017 CPS Public Use Microdata
library(tidyverse)
download.file("http://thedataweb.rm.census.gov/pub/cps/march/asec2017_pubuse.zip", "asec2017_pubuse.zip")
download.file("http://thedataweb.rm.census.gov/pub/cps/march/asec2016_pubuse_v3.zip", "asec2016_pubuse.zip")
download.file("http://thedataweb.rm.census.gov/pub/cps/march/asec2015_pubuse.zip", "asec2015_pubuse.zip")
unzip("asec2017_pubuse.zip", "asec2017_pubuse.dat")
unzip("asec2016_pubuse.zip", "asec2016_pubuse_v3.dat")
unzip("asec2015_pubuse.zip", "asec2015_pubuse.dat")
gendata <- function(flnm){
read_fwf(flnm,
fwf_cols(rectype=c(1,1),
hhseq=c(2,6),
sex=c(24,24),
hisp=c(31,31),
pob=c(84,86),
mpob=c(87,89),
fpob=c(90,92),
nativity=c(95,95),
basicwgt=c(139,146),
marchwgt=c(155,162)
), col_types = "cccccccccc") %>%
filter(rectype=="3") %>%
mutate(basicwgt = as.numeric(basicwgt)/100, marchwgt=as.numeric(marchwgt)/100) %>%
mutate(pob = as.numeric(pob), fpob=as.numeric(fpob),mpob=as.numeric(mpob)) %>%
mutate(generation = case_when(
nativity %in% c('4','5') ~ '1',
nativity %in% c('1','2','3') & mpob < 100 & fpob >=100 ~ '2',
nativity %in% c('1','2','3') & mpob >= 100 & fpob < 100 ~ '2',
nativity %in% c('1','2','3') & mpob >= 100 & fpob >= 100 ~ '2',
nativity %in% c('1','2','3') & mpob < 100 & fpob < 100 ~ '3',
TRUE ~ '99'))
}
pers15 <- gendata("asec2015_pubuse.dat")
pers16 <- gendata("asec2016_pubuse_v3.dat")
pers17 <- gendata("asec2017_pubuse.dat")
pers15 %>% group_by(generation) %>% summarize(n=sum(marchwgt)/1000)
pers16 %>% group_by(generation) %>% summarize(n=sum(marchwgt)/1000)
pers17 %>% group_by(generation) %>% summarize(n=sum(marchwgt)/1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment