Importing Police-Public Contact Surveys Into R

in #education6 years ago (edited)

The Police-Public Contact Survey (PPCS), which "provides detailed information on the characteristics of persons who had some type of contact with police during the year, including those who contacted the police to report a crime or were pulled over in a traffic stop," is a useful tool in examining trends like racial disparities surrounding police use of force or requests for police assistance. This survey is released about every three years, and this post is a walkthrough on how to import all years of this surveys into R. The output is a .Rda file for each year the survey was conducted.

policeCar.jpg

Because the Institute for Social Research requires a login to download data from these surveys, files for each year will have to be downloaded manually.

To download these files, navigate pages for all of the survey's years: 1999, 2002, 2005, 2008, 2011, and 2015. When more years of the survey are released, they will be listed in the National Crime Victimization Survey Series page. On the pages for the 1999 and 2002 iteratations of the survey, under the "Download" dropdown, chose the "SAS" option, which will download a zipped. For all other years, choose the "Delimited" option from the download dropdown. Note that while some years do have an "R" specific download option, not all of them do, and for consistency the "R" option is not used here. Do not unzip this file! Create a folder with the path "ppcs/raw" and drag all the downloaded zip folders into it. Then run the following code in R:

setwd('YOURDIRECTORY/ppcs/raw/')

#unzip files
files <- list.files()
sapply(files, unzip)
sapply(files, file.remove)

library(SAScii)

#SAS imports take a long time.
#Use a text editor to open .sas files, find, and define "INPUT" lines
#INPUT=1205
#define LRECL, which is given in each .sas file
#LRECL=1247
ppcs1999 <- read.SAScii("ICPSR_03151/DS0001/03151-0001-Data.txt", "ICPSR_03151/DS0001/03151-0001-Setup.sas", 1205, lrecl = 1247)

# LRECL=953
# INPUT=544
ppcs2002 <- read.SAScii("ICPSR_04273/DS0001/04273-0001-Data.txt", "ICPSR_04273/DS0001/04273-0001-Setup.sas", 544, lrecl = 953)

ppcs2005 <- read.table('ICPSR_20020/DS0001/20020-0001-Data.tsv', sep = '\t', header = TRUE, stringsAsFactors = FALSE, fill = TRUE)
ppcs2008 <- read.table('ICPSR_32022/DS0001/32022-0001-Data.tsv', sep = '\t', header = TRUE, stringsAsFactors = FALSE, fill = TRUE)
ppcs2011 <- read.table('ICPSR_34276/DS0001/34276-0001-Data.tsv', sep = '\t', header = TRUE, stringsAsFactors = FALSE, fill = TRUE)
ppcs2015 <- read.table('ICPSR_36653/DS0001/36653-0001-Data.tsv', sep = '\t', header = TRUE, stringsAsFactors = FALSE, fill = TRUE)

setwd('..')
dir.create('rda')
setwd('rda/')
save(ppcs2002,file="ppcs2002.Rda")
save(ppcs2005,file="ppcs2005.Rda")
save(ppcs2008,file="ppcs2008.Rda")
save(ppcs20011,file="ppcs2011.Rda")
save(ppcs2015,file="ppcs2015.Rda")



Now you should have a 'ppcsYYYY.Rda' file for each year in a folder named rda.

Note: this code works for all current years of data available on these surveys, but will have to be manually adjusted as new years are added. Be careful to check variable names for each year of data you plan to use.

Footer_somethingburger

Sort:  

Hello! Your post has been resteemed and upvoted by @ilovecoding because we love coding! Keep up good work! Consider upvoting this comment to support the @ilovecoding and increase your future rewards! ^_^ Steem On!

Reply !stop to disable the comment. Thanks!

Congratulations @somethingburger! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

Award for the number of upvotes

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @steemitboard:

SteemitBoard Ranking update - Resteem and Resteemed added

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @somethingburger! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You made more than 15000 upvotes. Your next target is to reach 16000 upvotes.

Click here to view your Board of Honor
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @steemitboard:

SteemFest³ - SteemitBoard Contest Teaser
The new Steemfest³ Award is ready!

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @somethingburger! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You made more than 16000 upvotes. Your next target is to reach 17000 upvotes.

Click here to view your Board of Honor
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @steemitboard:

Meet the Steemians Contest - The results, the winners and the prizes
Meet the Steemians Contest - Special attendees revealed

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @somethingburger! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You made more than 17000 upvotes. Your next target is to reach 18000 upvotes.

Click here to view your Board of Honor
If you no longer want to receive notifications, reply to this comment with the word STOP

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @somethingburger! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You made more than 18000 upvotes. Your next target is to reach 19000 upvotes.

Click here to view your Board
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @steemitboard:

Christmas Challenge - The party continues

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @somethingburger! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!