Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
阅读R中的消费者价格指数数据?_R - Fatal编程技术网

阅读R中的消费者价格指数数据?

阅读R中的消费者价格指数数据?,r,R,我需要将此链接中的数据读取到数据帧中。遗憾的是,数据不在csv文件中。最好的方法是什么 我试过了 cpiai <- read.table("http://www.bls.gov/cpi/cpifiles/cpiai.txt") cpiai坚持读表: path<-"http://www.bls.gov/cpi/cpifiles/cpiai.txt" cpiai <- read.table(path,header=T,fill=TRUE,skip=16) >head(cp

我需要将此链接中的数据读取到数据帧中。遗憾的是,数据不在csv文件中。最好的方法是什么

我试过了

cpiai <- read.table("http://www.bls.gov/cpi/cpifiles/cpiai.txt")

cpiai坚持读表:

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

path坚持读表:

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

<代码>路径> P> J.R.的答案给出了所有你需要知道的,但是为了完成,还考虑通过QuANDL来访问数据,这也为你提供了一个统一的(免费的)API。p> 对于该CPI,该页面非常简单,访问也非常容易

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> 
R>库(Quandl)
R> dat头(dat)
日期值
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>

J.R.的答案给了你所有你需要知道的,但是为了完成,还考虑通过Quand来访问数据,这也为你提供了一个统一的(免费的)API。p> 对于该CPI,该页面非常简单,访问也非常容易

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> 
R>库(Quandl)
R> dat头(dat)
日期值
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>

您只需根据您的文件设置
skip
sep
选项,您可以尝试
read.fwf
,但这看起来已经足够好了,您应该能够使用
read.table
(如上所述,使用
skip
和其他相关参数)。您只需根据文件设置
skip
sep
选项,就可以尝试
read.fwf
,但这看起来已经足够好了,您应该可以使用
read.table
(如上所述,使用
skip
和其他相关参数)。