[R] downsampling

Philipp Pagel p.pagel at wzw.tum.de
Fri Jul 24 13:12:56 CEST 2009


On Fri, Jul 24, 2009 at 03:16:58AM -0600, Warren Young wrote:
> Michael Knudsen wrote:
> >On Fri, Jul 24, 2009 at 9:32 AM, Jan Wiener<jan.wiener at tuebingen.mpg.de> wrote:
> >
> >>x=sample(1:5, 115, replace=TRUE)
> >>
> >>How do I downsample this vector to 100 entries? Are there any R
> >>functions or packages that provide such functionality.
> >
> >What exactly do you mean by downsampling?
> 
> It means that the original 115 points should be treated as a
> continuous function of x, or t, or whatever the horizontal axis is,
> with new values coming from this function at 100 evenly-spaced
> points along this function.

There probably is a proper function for that and some expert will
point it out. Until then I'll share my thoughts:

# make up some data
foo <- data.frame(x= 1:115, y=jitter(sin(1:115/10), 1000))
plot(foo)

# use approx for interpolation
bar <- approx(foo, n=30)
lines(bar, col='red', lwd=2)

# or use spline for interpolation
bar <- spline(foo, n=30)
lines(bar, col='green', lwd=2)

# or fit a loess curve
# had to play with span to make it look ok
model <- loess(y~x, foo, span=1/2)   
x <- seq(1, 115, length.out=30)
bar <- predict(model, newdata=data.frame(x=x, y=NA))
lines(x, bar, col='blue', lwd=2)


Jan, does that help a little?

cu
	Philipp

-- 
Dr. Philipp Pagel
Lehrstuhl für Genomorientierte Bioinformatik
Technische Universität München
Wissenschaftszentrum Weihenstephan
85350 Freising, Germany
http://webclu.bio.wzw.tum.de/~pagel/




More information about the R-help mailing list