Importing Column Delimited Text Data into Rstudio

Hello Rstudio world,
I'm new to Rstudio but not new to programming. I mainly deal with SAS but I'm looking to expand my horizons.
I've been unsuccessfully trying to import, what I consider, column delimited data into Rstudio.

Data:
http://thedataweb.rm.census.gov/pub/cps/basic/201701-/jun19pub.zip

Record Layout:
http://thedataweb.rm.census.gov/pub/cps/basic/201701-/January_2017_Record_Layout.txt

I don't know how to tell Rstudio (or tidyverse?) how to read x-columns with x-width and give it x-name. I tried going the 'Import Datasets' route but, the file is not space/whitespace delimited.

Appreciate if anyone can point me to any relevant documentation.

readr::read_fwf() should be what you are after.

To read from "input.txt" 3 columns of width 20, 10, and 12 respectively named "name", "state", "ssn" you would use.

library(readr)
read_fwf("input.txt", fwf_widths(c(20, 10, 12), c("name", "state", "ssn")))

But also you may want to look at special purpose packages to read census data. I am not familiar with the field, but maybe something like ipumsr would allow you to import the data you need?

4 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.