【问题标题】:Is there a R function to format a time series in percentage to numeric?是否有 R 函数可以将时间序列格式化为数字的百分比?
【发布时间】:2020-03-06 12:08:44
【问题描述】:

我有大量以百分比形式表示的兴趣时间序列。我需要将时间序列格式化为数字(百分比除以 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 ))

【问题讨论】:

  • 如果数据x太大,请用dput(x)dput(head(x, 20))发布数据示例。
  • 结构(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. 5.47), class= c("xts", "zoo"), .indexCLASS = "日期", tclass= "日期", .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= “日期” ), .Dim = c(20L, 1L ))

标签: r formatting xts


【解决方案1】:

这是一个自动执行问题中的任务的函数。

perc2num <- function(x) {
  if(is.character(x)){
    x <- ifelse(grepl('%', x), sub('%', '', x), 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)

【讨论】:

    【解决方案2】:

    我不确定问题可能是什么。 '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
    

    【讨论】:

      猜你喜欢
      • 2021-06-19
      • 2011-11-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多