How to Calculate Rolling Correlation in R? Last Updated : 20 Jun, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we will discuss Rolling Correlation in R Programming Language. Correlation is used to get the relationship between two variables. It will result in 1 if the correlation is positive.It will result in -1 if the correlation is negative.it will result in 0 if there is no correlation. Rolling correlations are used to get the relationship between two-time series on a rolling window. We can calculate by using rollapply() function, This is available in the zoo package, So we have to load this package. Syntax: rollapply(data, width, FUN, by.column=TRUE) where, data is the input dataframe.width is an integer that specifies the window width for the rolling correlation.FUN is the function to be applied.by.column is used to specify whether to apply the function to each column separately. We can get correlation using cor() function. Syntax: cor(column1,column2) Example 1: R program to calculate rolling correlation for the dataframe. R # load the library library(zoo) # create dataframe with 3 columns data = data.frame(day=1:15, col1=c(35:49), col2=c(33:47)) # display print(data) # get rolling correlation for col1 and # col2 with width 6 print(rollapply(data, width=6, function(x) cor(x[,2],x[,3]), by.column=FALSE)) Output: Example 2: R # load the library library(zoo) # create dataframe with 3 columns data = data.frame( col1=c(23,45,23,32,23), col2=c(1,45,67,32,45)) # display print(data) # get rolling correlation for col1 and # col2 with width 2 print(rollapply(data, width=2, function(x) cor(x[,1],x[,2]), by.column=FALSE)) Output: Comment More infoAdvertise with us Next Article How to Calculate Correlation By Group in R S sireeshakanneganti112 Follow Improve Article Tags : R Language R-Statistics Similar Reads How to Calculate Rolling Correlation in Python? Correlation generally determines the relationship between two variables. The rolling correlation measure the correlation between two-time series data on a rolling window Rolling correlation can be applied to a specific window width to determine short-term correlations. Calculating Rolling Correlati 2 min read How to Calculate Partial Correlation in R? In this article, we will discuss how to calculate Partial Correlation in the R Programming Language. Partial Correlation helps measure the degree of association between two random variables when there is the effect of other variables that control them. in partial correlation in machine learning It g 3 min read How to Calculate Cross Correlation in R? In this article we will discuss how to calculate cross correlation in R programming language. Correlation is used to get the relation between two or more variables. The result is 0, if there is no correlation between two variablesThe result is 1, if there is positive correlation between two variable 1 min read How to Calculate Polychoric Correlation in R? In this article, we will discuss how to calculate polychoric correlation in R Programming Language. Calculate Polychoric Correlation in R Correlation measures the relationship between two variables. we can say the correlation is positive if the value is 1, the correlation is negative if the value is 2 min read How to Calculate Correlation By Group in R Calculating correlation by group in R Programming Language involves finding the correlation coefficient between two variables within each subgroup defined by another variable. In R, correlation by group can be achieved by using the cor() function along with other functions like group_by() from the ' 5 min read How to Calculate Point-Biserial Correlation in R? In this article, we will discuss how to calculate Point Biserial correlation in R Programming Language. Correlation measures the relationship between two variables. we can say the correlation is positive if the value is 1, the correlation is negative if the value is -1, else 0. Point biserial correl 2 min read Like