[R] Time series and correlation

Ivan Krylov kry|ov@r00t @end|ng |rom gm@||@com
Wed Apr 27 08:35:01 CEST 2022


On Tue, 26 Apr 2022 20:13:55 -0800
Marlin Keith Cox <marlinkcox using gmail.com> wrote:

> cor(adf[,1], adf[,2])

> But if one of the columns is Time, it does not work.

Giving you an error that the column "must be a number", right? Which
class is your time column now?

There are different numeric representations for time. As long as they
only vary by the offset (did time start on 1970-01-01, or January 1,
4713 BC?) and scale (are we measuring seconds or days since epoch?),
the correlation should come out the same (subject to rounding errors),
but R doesn't automatically perform any conversion to a given
representation.

If your dates are currently character strings, use Sys.strptime or
something from the lubridate package to convert them into date objects.
Once you have date objects, convert them to numbers. For example, dates
stored as POSIXct objects consist of seconds since 1970-01-01, which
you can get by using as.numeric().

-- 
Best regards,
Ivan



More information about the R-help mailing list