较长的物体长度不是较短物体长度的倍数? [英] Longer object length is not a multiple of shorter object length?

查看:26
本文介绍了较长的物体长度不是较短物体长度的倍数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么 R 会警告我较长的对象长度不是较短对象长度的倍数"

I don't understand why R gives me a warning about "Longer object length is not a multiple of shorter object length"

我有这个对象,它是通过对 xts 系列进行聚合生成的,给出工作日中位数:

I have this object which is generated by doing an aggregate over an xts series giving the weekday median:

u <- aggregate(d, list(Ukedag = format(index(d),"%w")), median)

1 314.0
2 282.5
3 270.0
4 267.0
5 240.5

然后我尝试将其应用到我原来的 xts 系列中,它看起来像这样(只是更长)

Then I try to apply this to my original xts series, which looks like this (only a lot longer)

head(d)
2009-01-02 116
2009-01-05 256
2009-01-06 286

使用:

coredata(d) <- coredat(d) - u[format(index(d),"%w")];

这会导致警告.

目的是减去工作日平均值.尽管有警告,它似乎仍然有效,但我应该担心什么?

The intent is to subtract the weekday mean. It appears to work despite the warning, but what should I worry about?

apply.daily(d, function(x) coredata(x) - u[format(index(x), "%w")] )

我确实犯了一个严重的错误.这不会发出任何警告,我通过以下方式对其进行了测试:

I did indeed have a serious error. This doesn't give any warnings and I tested it by doing:

apply.daily(d, function(x) u[format(index(x), "%w")] )

然后检查了一些日期,发现与日历一致.

Then checking some dates, and it appeared that is was in alignment with the calendar.

推荐答案

是的,这是您应该担心的事情.使用 nrow() 检查对象的长度.R 可以自动复制对象,以便它们在不同时具有相同的长度,这意味着您可能正在对不匹配的数据执行操作.

Yes, this is something that you should worry about. Check the length of your objects with nrow(). R can auto-replicate objects so that they're the same length if they differ, which means you might be performing operations on mismatched data.

在这种情况下,您有一个明显的缺陷,即从原始数据中减去聚合数据.这些肯定会有不同的长度.我建议您将它们合并为时间序列(使用日期),然后是 locf(),然后进行减法运算.否则,通过将原始日期截断为与聚合系列相同的间隔来合并它们.只是要非常小心,不要放弃观察.

In this case you have an obvious flaw in that your subtracting aggregated data from raw data. These will definitely be of different lengths. I suggest that you merge them as time series (using the dates), then locf(), then do your subtraction. Otherwise merge them by truncating the original dates to the same interval as the aggregated series. Just be very careful that you don't drop observations.

最后,作为您开始时的一些一般建议:查看您的计算结果,看看它们是否有意义.您甚至可以将它们拉入电子表格并复制结果.

Lastly, as some general advice as you get started: look at the result of your computations to see if they make sense. You might even pull them into a spreadsheet and replicate the results.

这篇关于较长的物体长度不是较短物体长度的倍数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆