Loulou
Loulou

Reputation: 703

Interpolate value only for short gap

My vector a <- c(2,0,0,1,0, 3, 0, 0,0,0,5,6) The zeros represent the missing values

I want to interpolate missing values only if the succession of 0 is shorter than 4

I am looking for a function which allows me to perform this

The wished output is [1] 2.00 1.67 1.33 1.00 2.00 3.00 0.00 0.00 0.00 0.00 5.00 6.00

Upvotes: 0

Views: 248

Answers (1)

d.b
d.b

Reputation: 32558

library(zoo)
temp1 = na.approx(replace(a, a == 0, NA))
temp2 = inverse.rle(with(rle(a), list(values = replace(values, values == 0 & lengths < 4, NA),
                                      lengths = lengths)))
replace(temp2, is.na(temp2), temp1[is.na(temp2)])
# [1] 2.000000 1.666667 1.333333 1.000000 2.000000 3.000000 0.000000 0.000000
# [9] 0.000000 0.000000 5.000000 6.000000

The following (as commented by G.Grothendieck) is better

temp = na.approx(object = replace(a, a == 0, NA), maxgap = 3)
replace(temp, is.na(temp), 0)
# [1] 2.000000 1.666667 1.333333 1.000000 2.000000 3.000000 0.000000 0.000000
# [9] 0.000000 0.000000 5.000000 6.000000

Upvotes: 4

Related Questions