是否有R函数可以将时间序列格式化为数字的百分比?

问题描述 投票:1回答:1

我有一个很大的时间序列,以百分比表示。我需要将时间序列的格式设置为数字(百分比除以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 ))
r formatting xts
1个回答
0
投票

这里是使问题任务自动执行的功能。

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)
© www.soinside.com 2019 - 2024. All rights reserved.