In R, you can calculate the autocorrelation using acf() function from tseries package. Autocorrelation measures the degree of similarity between a time series and a lagged version of itself over successive time intervals.

The following method shows how you can use it with syntax.

Method: Use acf() Function

library(tseries)

acf(data)

The following example shows how to calculate autocorrelation in R.

Calculate AutoCorrelation Using acf() Function in R

Let’s see how we can calculate autocorrelation for numeric vector in R:

# Load library
library(tseries)

# Define data
Pressure <-c(12.39,11.25,12.15,13.48,13.78,12.89,12.21,12.58)

# Find autocorrelation
acf(Pressure,pl=FALSE)

Output:

Autocorrelations of series ‘Pressure’, by lag

     0      1      2      3      4      5      6      7 
 1.000  0.396 -0.407 -0.505 -0.108  0.102  0.021  0.001 

As in the output tThe autocorrelation at lag 0 is 1 and autocorrelation at lag 1 is 0.396.