# New R code for ‘moving’ or ‘running’ correlations

[This article was first published on

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

**ikan bilis » R**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Someone at the lab asked how to ‘do something like running means, but with correlations’. I couldn’t find any existing code that would make a good example, so I just wrote some myself.

It would be nice to do this without looping. If anyone has a clever way to do this, please do let me know.

# 2011-03-04 # v0.01 MovingCor <- function(x, y, window.size=21, method="pearson") { # Computes moving correlations between two vectors with symmetrical windows. # # Args: # x: One of the two vectors whose correlation is to be calculated. # y: The other vector. Note that it must be of the same length as x. # window.size: The size of windows to be used for each calculated # correlation. Note that if even numbers are chosen, the # window will not be skewed as there will be one extra value # on the upper-side of the window. Default size is 21. # method: The method of correlation. May be: "pearson", "kendall", or # "spearman". Default is "pearson". # # Returns: # A vector of the moving correlations. n <- length(x) # Setup a few catches for error handling. if (TRUE %in% is.na(y) || TRUE %in% is.na(x)) { stop("Arguments x and y cannot have missing values.") } if (n <= 1 || n != length(y)) { stop("Arguments x and y have different lengths: ", length(x), " and ", length(y), ".") } out <- rep(NA, round(window.size/2)) # Stuffing the returned vector. for (value in seq(from = 1, to = n - (window.size - 1))) { value.end <- value + (window.size - 1) out <- append(out, cor(x[value:value.end], y[value:value.end], method = method)) } out <- append(out, rep(NA, n - length(out))) # Finish stuffing. return(out) }

**EDIT:**

There are more nimble functions out there for this, and other window-related tasks. See the `caTools`‘s `runmean` function. The package `zoo` also has a number of quick functions including `rollmean` and the more general `rollapply`.

To

**leave a comment**for the author, please follow the link and comment on their blog:**ikan bilis » R**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.