Florian Oswald
Florian Oswald

Reputation: 5134

read.table unable to read tab delimited file?

I'm having trouble reading this table into R:

http://www.census.gov/popest/about/geo/state_geocodes_v2012.txt

I tried all of the following:

read.table("http://www.census.gov/popest/about/geo/state_geocodes_v2012.txt")
read.table("http://www.census.gov/popest/about/geo/state_geocodes_v2012.txt",skip=7,header=FALSE)
read.table("http://www.census.gov/popest/about/geo/state_geocodes_v2012.txt",skip=8,header=FALSE)
read.table("http://www.census.gov/popest/about/geo/state_geocodes_v2012.txt",skip=10,header=FALSE)

If I tell it that the separator is a tab, i get the wrong table:

d = read.table(file="http://www.census.gov/popest/about/geo/state_geocodes_v2012.txt",header=FALSE,skip=7,sep="\t")

the only thing that seems to work is readLines. but then i don't know how to get a data.frame out of each line.

d =readLines("http://www.census.gov/popest/about/geo/state_geocodes_v2012.txt")

any suggestions? thanks.

Upvotes: 0

Views: 254

Answers (1)

Carl Witthoft
Carl Witthoft

Reputation: 21502

I agree that read.fwf will work, once you've worked out the widths.

But, Yeah -- I just hate people who allow whitespace inside elements (e.g. "SouthDakota" ) . One other thing you can do is edit the source text file, replacing {2,N} spaces with a tab. That will leave the state names as-is but give you a workable delimiter.

Upvotes: 1

Related Questions