use*_*629 15 csv r import-from-csv read.table
我正在尝试从IRS读取.csv文件,它似乎没有以任何奇怪的方式格式化.
我正在使用这个read.table()
功能,我过去曾多次使用过这个功能,但这次没用了; 相反,我得到这个错误:
data_0910<-read.table("/Users/blahblahblah/countyinflow0910.csv",header=T,stringsAsFactors=FALSE,colClasses="character")
Error in read.table("/Users/blahblahblah/countyinflow0910.csv", :
more columns than column names
Run Code Online (Sandbox Code Playgroud)
它为什么这样做?
作为参考,.csv
可以在以下位置找到这些文件:
http://www.irs.gov/uac/SOI-Tax-Stats-County-to-County-Migration-Data-Files
(我需要的是县到县迁移.csv部分 - 流入或流出.)
Mat*_*erg 19
它使用逗号作为分隔符.所以你可以设置sep=","
或只使用read.csv
:
x <- read.csv(file="http://www.irs.gov/file_source/pub/irs-soi/countyinflow1011.csv")
dim(x)
## [1] 113593 9
Run Code Online (Sandbox Code Playgroud)
该错误是由某些值中的空格和不匹配的引号引起的.标题中没有空格,因此read.table
认为有一列.然后它认为它在某些行中看到多个列.例如,前两行(标题和第一行):
State_Code_Dest,County_Code_Dest,State_Code_Origin,County_Code_Origin,State_Abbrv,County_Name,Return_Num,Exmpt_Num,Aggr_AGI
00,000,96,000,US,Total Mig - US & For,6973489,12948316,303495582
Run Code Online (Sandbox Code Playgroud)
和不匹配的引号,例如在第1336行(第1335行),它将read.table
与默认quote
参数混淆(但不是read.csv
):
01,089,24,033,MD,Prince George's County,13,30,1040
Run Code Online (Sandbox Code Playgroud)