本文介绍了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.

通过您的编辑,我现在明白您要做什么了.您想要差异,但您希望每年 1 月作为该月的水平,而不是与上一年 12 月的差异.

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