Shanshan Chen

Handling Time Series in R

For intensive longitudinal data (e.g. with a sampling rate more than 1/15 Hz), I recommend moving to Matlab for data visualization. Still, it’s useful to know how to handle time series in R.

library(chron)
library(zoo)
library(readxl)
DF<- read_excel("data.xlsx", sheet ="Sheet 1")
DF_temp = read_excel("data.xlsx", sheet="Sheet 1",col_types = "date") ## keep time and date variables as they are
DF$Time = DF_temp$Time # replace time variable in the original data frame to time format
DF$Date = DF_temp$Date # replace date variable in the original data frame to time format
DF<- read.csv("data.csv")
DF$Time <- as.character(DF$Time, format="%H:%M:%S" )
DF$Tvec <- chron(times=DF$Time)
DF$ToD_hrs <- hours(DF$Tvec)+minutes(DF$Tvec)/60
DF$ToD_mins <- 60*hours(DF$Tvec)+minutes(DF$Tvec)