本文介绍了xts :: apply错误:"coredata.xts(x)中的错误:当前不支持的数据类型"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试执行以下工作时,我发生了错误:

The error occurred to me When I was trying to do the following work:

# generate random integrals #
data <- xts(floor(runif(100, 1,101)),as.Date("1973-02-01") + c(1:100) - 1)
apply.monthly(data, diff,1,1)

,而这一方法有效:

apply.monthly(data,mean)

我已经检查了类似的问题,但似乎不适用于这里的情况.

I have checked similar questions posted, but it seems they do not apply to the situation here.

有什么建议吗?

一些进一步的解释:

我需要这个的原因是我得到了如下的时间序列数据集,

The reason I need this is that I got a time series data set like the following,

1990-05 100
1990-04 80
1990-03 60
1990-02 20
1990-01 5
1989-12 110
1989-11 89
1989-10 78
...

每年,y(t)=y_(t-1)+dy,其中dt是时段t中的值变化.但是,这种模式仅在每年和每年分别发生.因此,基本上,我想检索每个特定年份的每个月之间的差额,即:

In each year, y(t)=y_(t-1)+dy, where dt is value change in period t. But this pattern only happens in each year and each year separately. So basically, I want to retrieve the difference between each month in every specific year, that is:

1990-05 20  #100-80
1990-04 20  #80-60
1990-03 40  #60-20
1990-02 15  #20-5
1990-01 5   #5
1989-12 21  #110-89
1989-11 11  #89-78  
...

希望我的解释已经很清楚了.

Hope I have made the explanation clear enough.

谢谢

推荐答案

apply.monthlyperiod.apply用于将数据聚合到指定时间段. diff不起作用,因为diff.xts返回的向量的长度与输入的长度相同. mean之所以起作用,是因为它为给定的输入向量返回了一个值.

apply.monthly and period.apply are used to aggregate data to the specified period. diff doesn't work because diff.xts returns a vector the same length as the input. mean works because it returns one value for a given input vector.

我不清楚您希望apply.monthly(data, diff)做什么.就像调用diff(data)然后将NA添加到每个月的第一个值一样.

It's not clear to me what you expect apply.monthly(data, diff) to do. It would be the same as calling diff(data) and then adding NA to the first value of each month.

通过您的编辑,我现在了解您正在尝试执行的操作.您需要差异,但您希望每年的一月成为该月的水平,而不是与上一年的十二月的差异.

With your edit, I now understand what you are trying to do. You want the differences, but you want January of each year to be the level for that month, not the difference from December of the prior year.

这是一种方法:

# Load your data as an example
Lines <- 
"1990-05 100
1990-04 80
1990-03 60
1990-02 20
1990-01 5
1989-12 110
1989-11 89
1989-10 78"
con <- textConnection(Lines)
# Ensure the timezone of your non-intraday xts object is UTC,
# or bad things can happen
x <- as.xts(read.zoo(con, FUN=as.yearmon), tzone="UTC")
close(con)

# Create a helper function
f <- function(x) {
  y <- diff(x)
  if (.indexmon(y)[1] == 0)
    y[1] <- x[1]
  y
}
# apply the function to each year subset and rbind the results
do.call(rbind, lapply(split(x,'years'), f))

这是另一种方式,您可能会发现更具吸引力.

Here's another way, that you might find more appealing.

colnames(x) <- "level"
# calculate all differences
x$diff <- diff(x$level)
# set January differences to their respective level
jan <- .indexmon(x) == 0
x[jan, "diff"] <- x[jan, "level"]

这篇关于xts :: apply错误:"coredata.xts(x)中的错误:当前不支持的数据类型"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 04:24