SlidingWindow <- function(FUN, data, window, step, strict){ # Validation testing if(strict) { if(!is.numeric(data)) stop("Please supply numeric data") if(sum(is.vector(data), is.matrix(data)) == 0) { stop("You must supply data as a vector or matrix") } if(is.vector(data)){ if(window > length(data)) stop("Window size is too large") if((step + window) > length(data)) stop("Window and step size should be small enough that multiple windows can be examined") } if(is.matrix(data)){ if(window > nrow(data)) stop("Window size is too large for number of rows") if(window > ncol(data)) stop("Window size is too large for number of cols") if((step + window) > nrow(data)) stop("Window and step size should be small enough that multiple windows can be examined") } } # code for vectors if(is.vector(data)) { total <- length(data) spots <- seq(from = 1, to = (total - window), by = step) result <- vector(length = length(spots)) for(i in 1:length(spots)){ result[i] <- match.fun(FUN)(data[spots[i]:(spots[i] + window - 1)]) } } # code for matrices if(is.matrix(data)){ total.x <- ncol(data) spots.x <- seq(from = 1, to = (total.x - window), by = step) total.y <- nrow(data) spots.y <- seq(from = 1, to = (total.y - window), by = step) result <- matrix(, length(spots.y), length(spots.x)) for(i in 1:length(spots.y)){ for(j in 1:length(spots.x)){ result[i, j] <- match.fun(FUN)(data[spots.y[i]:(spots.y[i] + window - 1), spots.x[j]:(spots.x[j] + window - 1)]) } } } # complete failure message if(!exists("result")) stop("Hmmm unknown error... Sorry") # return the result to the user return(result) }
Here is an example of this in action. First, let us simulate a bit of interesting data:
x <- seq(-pi, pi, len = 600) y <- seq(-pi, pi, len = 600) g <- expand.grid(x = x, y = y) g$z <- sin(sqrt(g$x^2 + g$y^2)) + rnorm(600^2, sd=2) ex.dat <- matrix(g$z,600,600) image(ex.dat)
This data looks like this when we plot it as a heat map:
reduced.ex.dat <- SlidingWindow(FUN="mean", data=ex.dat, window=10, step=5, strict=T) image(reduced.ex.dat)
And now we can produce the same plot on our data after applying a sliding window:
Hopefully, that is helpful!
ReplyDeleteThank you for your post. This is excellent information. It is amazing and wonderful to visit your site.
upvc sliding windows
upvc doors manufacturers
upvc sliding doors
upvc casement windows suppliers in hyderabad
upvc windows
ReplyDeleteThank you for your post. This is excellent information. It is amazing and wonderful to visit your site.
upvc sliding doors
upvc casement windows suppliers in hyderabad
upvc doors manufacturers
Top upvc windows dealers in hyderabad
upvc casement windows manufacturers hyderabad
We square measure a quality-committed company that helps us earned the name as top-notch Manufacturer and Supplier of Modular Kitchen and Kitchen Cabinet. Browse our range and if you've got any personalized demand, be happy to share with us. We’ll be happy to serve you the merchandise, you’ve been checking out.
ReplyDeleteModular Kitchen
Kitchen Cabinet in Hyderabad
Manufacturer and Supplier of Mosquito Mesh Window
Manufacturer and Supplier of Sliding Window in Hyderabad
Thanks for your post. It’s truly informative. Will surely keep visiting this website.
ReplyDeleteupvc sliding windows.
upvc sliding doors in hyderabad
upvc sliding doors manufacturers
Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.
ReplyDeleteupvc sliding door suppliers in hyderabad
upvc sliding door dealers
upvc sliding doors dealers Hyderabad
upvc sliding doors manufacturers hyderabad
upvc sliding doors
upvc doors suppliers in hyderabad
upvc doors manufacturers in hyderabad
ReplyDeleteYour blog is very nice thank you for this wonderful information i like very much for this article. and it will give the more information from this blog.
sliding door soft closer
Thanks for sharing such beautiful and informative post.I have some interesting article for viewers. Visit
ReplyDeleteOnline service provider
Online Night Services
Online 24*7 service provider
Online service
Entertainment services
Online Entertainment
Thank you for sharing this.
ReplyDeleteManufacturing High Quality Residential & Commercial Doors and Windows, well recognized & reputed by homeowners, renovators, architects and builders. Proudly Australian owned & operated for over 60 years.Call us now at 9602 8773 for an obligation free Quote.
Sliding windows