我需要将此链接http://www.bls.gov/cpi/cpifiles/cpiai.txt中的数据读入数据框。遗憾的是,数据不在csv文件中。这样做的最佳方式是什么?
我试过
cpiai <- read.table("http://www.bls.gov/cpi/cpifiles/cpiai.txt")
谢谢!
答案 0 :(得分:2)
坚持阅读。表:
path<-"http://www.bls.gov/cpi/cpifiles/cpiai.txt"
cpiai <- read.table(path,header=T,fill=TRUE,skip=16)
>head(cpiai)
Year Jan. Feb. Mar. Apr. May June July Aug. Sep. Oct. Nov. Dec. Avg. Dec Avg
1 1913 9.8 9.8 9.8 9.8 9.7 9.8 9.9 9.9 10.0 10.0 10.1 10.0 9.9 NA NA
2 1914 10.0 9.9 9.9 9.8 9.9 9.9 10.0 10.2 10.2 10.1 10.2 10.1 10.0 1.0 1.0
3 1915 10.1 10.0 9.9 10.0 10.1 10.1 10.1 10.1 10.1 10.2 10.3 10.3 10.1 2.0 1.0
4 1916 10.4 10.4 10.5 10.6 10.7 10.8 10.8 10.9 11.1 11.3 11.5 11.6 10.9 12.6 7.9
5 1917 11.7 12.0 12.0 12.6 12.8 13.0 12.8 13.0 13.3 13.5 13.5 13.7 12.8 18.1 17.4
6 1918 14.0 14.1 14.0 14.2 14.5 14.7 15.1 15.4 15.7 16.0 16.3 16.5 15.1 20.4 18.0
答案 1 :(得分:2)
JR的答案为您提供了所有您需要知道的知识,但为了完整,还要考虑通过Quandl进行访问,该数据管理数据和还为您提供了数百万的统一(免费)API他们追踪的系列。
对于此CPI,页面为https://www.quandl.com/FRED/CPIAUCSL-Consumer-Price-Index-for-All-Urban-Consumers-All-Items-USA-Inflation,访问权限就像
一样简单R> library(Quandl)
R> dat <- Quandl("FRED/CPIAUCSL")
R> head(dat)
Date Value
1 2014-09-01 237.633
2 2014-08-01 237.428
3 2014-07-01 237.909
4 2014-06-01 237.693
5 2014-05-01 237.083
6 2014-04-01 236.254
R>