Skip to content

Instantly share code, notes, and snippets.

@morganmelon
Last active November 15, 2016 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morganmelon/13309827610c97229f21d7e982f9e258 to your computer and use it in GitHub Desktop.
Save morganmelon/13309827610c97229f21d7e982f9e258 to your computer and use it in GitHub Desktop.
Correction factor for black men and all men 1990
#Calculating rates for Correctional vs Other Institution
#load packages
library(readr)
library(dplyr)
#read in data extract
ipums <- read_csv('data/FINALFINALDATA.csv', col_types = cols(PERWT=col_double()))
a <- ipums %>% filter((AGE>=15 & AGE<=70) & (STATEFIP!=11) & ((!(STATEFIP %in% c(2,15)) | YEAR >=1960)))
#filter out Female
e <- a %>% filter(SEX==1)
#data frame for Prison prior to 1990
prison1990 <- e %>% filter(GQTYPE==2) %>% group_by(YEAR) %>% summarise(NumPrison=sum(PERWT))
#data frame for All Institutions to 1990
inst1990 <- e %>% filter(YEAR<=1980 & (GQTYPE==2 | GQTYPE==3 | GQTYPE==4)) %>% group_by(YEAR) %>% summarise(NumGQ=sum(PERWT))
#joining data frame
all1990 <- left_join(prison1990, inst1990)
pct1990 <- all1990 %>% mutate(Percent=NumPrison/NumGQ)
#export data
write_csv(pct1990, 'correctionfactor_all.csv')
#Calculating rates for Correctional vs Other Institution for just Black Men
#load packages
library(readr)
library(dplyr)
#read in data extract
ipums <- read_csv('data/FINALFINALDATA.csv', col_types = cols(PERWT=col_double()))
a <- ipums %>% filter((AGE>=15 & AGE<=70) & (STATEFIP!=11) & ((!(STATEFIP %in% c(2,15)) | YEAR >=1960)))
#recode RACESING
b <- a %>% mutate(Race = factor(ifelse(RACESING==2, 1,
ifelse(RACESING==1, 2, 3)),
labels = c('Black', 'White', 'Other')))
#filter for males and Black
e <- b %>% filter(SEX==1 & Race=='Black')
#data frame for Prison prior to 1990
prison1990 <- e %>% filter(GQTYPE==2) %>% group_by(YEAR) %>% summarise(NumPrison=sum(PERWT))
#data frame for All Institutions to 1990
inst1990 <- e %>% filter(YEAR<=1980 & (GQTYPE==2 | GQTYPE==3 | GQTYPE==4)) %>% group_by(YEAR) %>% summarise(NumGQ=sum(PERWT))
#joining data frame
all1990 <- left_join(prison1990, inst1990)
pct1990 <- all1990 %>% mutate(Percent=NumPrison/NumGQ)
#export data
write_csv(pct1990, 'correctionfactor_black.csv')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment