是否有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,6.06,5.89,5.75,5.72,5.5,5.33,5.29,5.28,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),tclass = "Date"),.Dim = c(20L,1L ))
bmw3i8is 回答:是否有R函数可以将时间序列格式化为百分比格式?

这是一个使问题中的任务自动化的功能。

perc2num <- function(x) {
  if(is.character(x)){
    x <- ifelse(grepl('%',x),sub('%','',x)
    as.numeric(x)/100
  }else if(is.numeric(x)){
    x/100
  }
}

a <- c(12,34.5,83.7)
b <- c('12','34.5','83.7')
d <- c('12%','34.5%','83.7 %')

perc2num(a)
perc2num(b)
perc2num(d)

由于NBdata3YS和其他系列都是"xts"对象,因此只需以该对象作为参数调用函数即可。

perc2num(NBdata3YS)
,

我不确定可能是什么问题。 “ 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
本文链接:https://www.f2er.com/3128491.html

大家都在问