NA的rollmeanr问题

时间:2018-08-13 15:11:34

标签: r zoo moving-average

作为基础,让:

import re

# trying to extract 'type', 'count' and 'destinations'.
# string1 has all keys and a single re.match works
# string2 is missing 'count'... any suggestions?

string1 = """
Name: default
type = Route
status = 0
count = 5
enabled = False
start_time = 18:00:00
end_time = 00:00:00
destinations = default
started = False
"""

string2 = """
Name: default
type = Route
status = 0
enabled = False
start_time = 18:00:00
end_time = 00:00:00
destinations = default
started = False
"""


pattern = re.compile(r"(?s).*type = (?P<type>\S*).*count = (?P<count>\S*).*destinations = (?P<destinations>\S*)")

m1 = re.match(pattern,string1)
# m1.groupdict() == {'type': 'Route', 'count': '5', 'destinations': 'default'}

m2 = re.match(pattern,string2)
# m2 == None

但是如果基础中有library(zoo) x1=c(1,2,3,4) x2=rollmeanr(x1,2,fill=NA) x=cbind(x1,x2) x1 x2 [1,] 1 NA [2,] 2 1.5 [3,] 3 2.5 [4,] 4 3.5 ,则NA不再起作用:

rollmeanr

x1[1]=NA x3=rollmeanr(x1,2,fill=NA) cbind(x1,x2,x3) x1 x2 x3 [1,] NA NA NA [2,] 2 1.5 NA [3,] 3 2.5 NA [4,] 4 3.5 NA 应该为x3[3],因为它具有2个先例数据。
有办法解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

zoo更改了rollmean在版本1.8-2(https://cran.r-project.org/web/packages/zoo/NEWS)周围的行为:

  

版本1.8-2中的更改

     
      
  • rollmean(x,k,...)方法现在在以下位置调用rollapply(x,k,(mean),...)   case x包含任何NA(因为rollmean中基于快速求和的解决方案是   在这种情况下不适用)。与rollsum()和rollmedian()类似。   (由Jan Gorecki报道。)
  •   

所以您看到的行为是1.7版;如果您升级到1.8(我猜@phiver已经完成),那么您应该会看到预期的结果。