首页 > 解决方案 > 是否有 R 函数可以将时间序列格式化为数字的百分比?

问题描述

我有大量以百分比格式表示的兴趣时间序列。我需要将时间序列格式化为数字(百分比除以 100)而不是百分比。有这样做的功能吗?

我的时间序列:


NBdata10YS=xts(x=NBdata10YS$OBS_VALUE,order.by = as.Date(as.yearmon(NBdata10YS$TIME_PERIOD)))

NBdata5YS=xts(x=NBdata5YS$OBS_VALUE,order.by = as.Date(as.yearmon(NBdata5YS$TIME_PERIOD)))

NBdata3YS=xts(x=NBdata3YS$OBS_VALUE,order.by = as.Date(as.yearmon(NBdata3YS$TIME_PERIOD)))

StatsobligasjonerNB=merge(NBdata10YS,NBdata5YS, NBdata3YS)

数据。

NBdata3YS <-
structure(c(6.03, 5.65, 5.94, 6.19, 6.03, 5.95, 
5.95, 6.06, 5.89, 5.75, 5.72, 5.5, 5.33, 5.29, 
5.28, 5.33, 5.5, 5.47, 5.4, 5.47 ), class = c("xts", 
"zoo"), .indexCLASS = "Date", tclass = "Date", 
.indexTZ = "UTC", tzone = "UTC", index = 
structure(c(852076800, 854755200, 857174400, 859852800, 
862444800, 865123200, 867715200, 870393600, 873072000, 
875664000, 878342400, 880934400, 883612800, 886291200, 
888710400, 891388800, 893980800, 896659200, 899251200, 
901929600), tzone = "UTC", tclass = "Date"), 
.Dim = c(20L, 1L ))

标签: rformattingxts

解决方案


我不确定问题可能是什么。'zoo' 或 'xts' 类的对象实际上是具有特殊索引属性的数字矩阵。因此,只需使用除法运算符就足够了:

library(xts)
NBdata3YS/100
             [,1]
1997-01-01 0.0603
1997-02-01 0.0565
1997-03-01 0.0594
1997-04-01 0.0619
1997-05-01 0.0603
1997-06-01 0.0595
1997-07-01 0.0595
1997-08-01 0.0606
1997-09-01 0.0589
1997-10-01 0.0575
1997-11-01 0.0572
1997-12-01 0.0550
1998-01-01 0.0533
1998-02-01 0.0529
1998-03-01 0.0528
1998-04-01 0.0533
1998-05-01 0.0550
1998-06-01 0.0547
1998-07-01 0.0540
1998-08-01 0.0547

这也适用于合并的 xts 时间序列:

M <- merge(NBdata3YS,NBdata3YS) 
#-------------
> M/100
           NBdata3YS NBdata3YS.1
1997-01-01    0.0603      0.0603
1997-02-01    0.0565      0.0565
1997-03-01    0.0594      0.0594
1997-04-01    0.0619      0.0619
1997-05-01    0.0603      0.0603
1997-06-01    0.0595      0.0595
1997-07-01    0.0595      0.0595
1997-08-01    0.0606      0.0606
1997-09-01    0.0589      0.0589
1997-10-01    0.0575      0.0575
1997-11-01    0.0572      0.0572
1997-12-01    0.0550      0.0550
1998-01-01    0.0533      0.0533
1998-02-01    0.0529      0.0529
1998-03-01    0.0528      0.0528
1998-04-01    0.0533      0.0533
1998-05-01    0.0550      0.0550
1998-06-01    0.0547      0.0547
1998-07-01    0.0540      0.0540
1998-08-01    0.0547      0.0547

推荐阅读