# [R] completing missing samples

Joshua Wiley jwiley.psych at gmail.com
Sat Sep 17 22:43:59 CEST 2011

```Hi Eran,

You have already gotten some suggestions from Michael, but I think
that Rich is correct to question the rational.  Any mechanism you
choose to replace the missing values will impose its structure on the
data. Veritate ab absurdo:

## data
x <- sin(seq(1, 17, .1)) + seq(-.5, .5, length.out = 161)
y <- x <- ts(x, start = Sys.Date(), end = Sys.Date() + 160)

## add missing values to y
y[-c(1, length(x))] <- NA
## interpolate linearlly
y[-length(y)] <- cumsum(c(y, rep(diff(y[!is.na(y)])/sum(is.na(y)),
sum(is.na(y)))))

## plot
dev.new(height = 5, width = 10)
par(mfrow= c(1, 2))
plot(y, ylim = range(x))
plot(x, ylim = range(x))

Cheers,

Josh

On Mon, Sep 12, 2011 at 1:42 AM, Eran Eidinger <eran at taykey.com> wrote:
> Hello,
>
> I have a time-series that has some missing samples.
> I was thinking on completing them using either zero-order hold or linear
> interpolation.
> I am looking for an efiicient way (other than a loop...) of identifiying the
> missing time slots and filling them.
>
> Can you think of any methods that might help here? (obviously
> which(diff(time)>min(diff(time))) will give the locations, but what
> then....?)
>
> Thanks,
> Eran.
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

--
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, ATS Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/

```