[R] Resampling physiological data using R?

Gabor Grothendieck ggrothendieck at gmail.com
Fri Dec 12 02:41:35 CET 2008

```Here some sample code to interpolate your data using zoo:
by5 is a sequence for every 5 time units which we merge
with z, the original data.  Then we use na.approx to replace
all NAs with linear interpolations.

Lines <- "Time    CursorX CursorY Pupilsize
1811543 -1      -1      -1
1811563 -1      -1      -1
1811584 511     370     4.175665
1811603 511     368     4.181973
1811624 521     368     4.210732
1811644 512     377     4.149632
1811664 524     377     4.275845
1811684 518     368     4.236212
1811703 516     370     4.238384
1811725 507     364     4.181157
1811744 509     371     4.185016
1811764 509     377     4.231987
1811784 514     387     4.252449
1811802 515     388     4.273726"

library(zoo)

by5 <- seq(5 * floor(start(z)/5), ceiling(5 * ceiling(end(z)/5)), 5)
zz <- merge(z, zoo(, as.integer(by5)))
zza <- na.approx(zz)

On Thu, Dec 11, 2008 at 3:38 PM, tsunhin wong <thjwong at gmail.com> wrote:
> Dear all R users,
>
> I am going to use R to process some of my physiological data about eye.
>
> The problem is the recording machine does not sample in a reliably
> constant rate: the time intervals between data sampled can vary from
> 9msec to ~120msec, while most around in the 15-30msec range.
> The below is a fraction of a single data file of a trial:
>
> Time    CursorX CursorY Pupilsize
> 1811543 -1      -1      -1
> 1811563 -1      -1      -1
> 1811584 511     370     4.175665
> 1811603 511     368     4.181973
> 1811624 521     368     4.210732
> 1811644 512     377     4.149632
> 1811664 524     377     4.275845
> 1811684 518     368     4.236212
> 1811703 516     370     4.238384
> 1811725 507     364     4.181157
> 1811744 509     371     4.185016
> 1811764 509     377     4.231987
> 1811784 514     387     4.252449
> 1811802 515     388     4.273726
>
> My goal is to "resample" these data so that the "Time" column
> increments by a regular interval, and the other columns of data are
> the averages (or estimates) at the point in time according to
> available data points.
> I have done something that I use a regular interval that is larger
> than the naturally occurring record machine, i.e. > 120msec for
> example, and acquire an average of the available data points for any
> particular regular time interval.
>
> Now, I need to achieve resampling for smaller regular interval: i.e.
> 5msec intervals, and interpolate / intrapolate the missing data points
> from the available ones.
> i.e. I may have to split up data points into the number of the regular
> intervals that it may occupied in time.
>
> Do you know if there is any package that is doing something similar?
> And because of the size of the data and computational demand (1500
> files each with 2000-8000+ lines), can you suggest me some
> (algorithmically) more efficient way of doing this?
>
> Thanks a lot!
>
> Regards,
>
>      John
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help