计算运行中位数时缺少值? [英] missing value when calculating running medians?
问题描述
我想消除一个时间序列,以避免虚假的抖动/错误.换句话说,我想进行一些非常局部的鲁棒平滑处理.
I would like to smooth out a time series to avoid spurious jitter/error. In other words I want to do some very local robust smoothing.
我在动物园包装中遇到了rollmean和rollmedian,但是遇到了问题,因为我的载体中有NA.然后我在某个地方读到那些动物园功能使用了runmed,这就是问题所在.
I came across rollmean and rollmedian in the zoo package but ran into a problem because my vector had a NA in it. I then read somewhere that those zoo functions use runmed and therein lies the problem.
==示例==
median(c(1,1,1,2,2,2,7,NA,1,2,3,10,10,10),na.rm = TRUE)
runmed(c(1,1,1,2,2,2,7,NA,1,2,3,10,10,10),k=3)
第一行返回2,但如果不包括na.rm = TRUE
,则返回NA.第二行返回Error in runmed(c(1, 1, 1, 2, 2, 2, 7, NA, 1, 2, 3, 10, 10, 10), k = 3) :
NA/NaN/Inf in foreign function call (arg 1)
.无法在行中添加na.rm参数.
The first line returns 2, but would have returned NA if na.rm = TRUE
was not included. The second line returns Error in runmed(c(1, 1, 1, 2, 2, 2, 7, NA, 1, 2, 3, 10, 10, 10), k = 3) :
NA/NaN/Inf in foreign function call (arg 1)
. There is no way to add a na.rm argument to the line.
如何运行以处理NA?顺便说一句,rollmean返回的向量在NA之前都是正确的,然后为每个值返回NA.
How can I get runmed to handle the NA? By the way, rollmean returns a vector which is correct up to the NA and then returns NA for every value thereafter.
推荐答案
使用na.omit
runmed(na.omit(c(1,1,1,2,2,2,7,NA,1,2,3,10,10,10)),k=3)
# [1] 1 1 1 2 2 2 2 2 2 3 10 10 10
#attr(,"k")
#[1] 3
或使用 zoo 中的na.*
函数之一(na.locf
,na.approx
,na.spline
,na.aggregate
等)
例如
Or use one of the na.*
functions from zoo (na.locf
, na.approx
, na.spline
, na.aggregate
, etc)
e.g.
runmed(na.locf(c(1,1,1,2,2,2,7,NA,1,2,3,10,10,10)),k=3)
#[1] 1 1 1 2 2 2 7 7 2 2 3 10 10 10
#attr(,"k")
#[1] 3
这篇关于计算运行中位数时缺少值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!