简体   繁体   English

将NA插入r中的时间序列对象

[英]insert NA into a time series object in r

i want to sum the months for all the years in a time series that looks like 我想在一个看起来像这样的时间序列中总结所有年份的月份

         Jan Feb Mar Apr Jun Jul Aug Sep Oct Nov Dec
2006              4   4   3    4   4   5   5   3  3
2007      3   3   2   2   4    3   3   2   2   5  5
2008      3   3   3   2   2    4   4   3   

by using window(the time series object,start=c(2006,3),end=c(2008,3),frequency=1) this line gives you a new ts object with just march of 2006-2007. 通过使用window(时间序列对象,start = c(2006,3),end = c(2008,3),frequency = 1),此行为您提供了一个新的ts对象,其行进时间为2006-2007。 However this does not work when the month does not have any values in it, is there any way to replace the gaps with NA? 但是,当月份中没有任何值时,此方法将不起作用,是否可以用NA替代差距? I have seen questions like this before but the dont answer i think for a ts object. 我以前见过这样的问题,但是我认为对于ts对象来说没有答案。

Assuming that 假如说

the_time_series_object <- ts(1:31, frequency = 12, start = c(2006, 3))

then: 然后:

window(the time series object, start = c(2006,3), end = c(2008,3), frequency = 12)

Your frequency should be 12 instead of 1. There's no NA problem it's just that one variable that you have wrong 您的频率应该是12而不是1。不存在NA问题,只是您错了一个变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM