Skip to content

Instantly share code, notes, and snippets.

@knbknb
Last active January 30, 2019 20:06
Show Gist options
  • Save knbknb/7b11673f928f93cfd1ade633dc52d84c to your computer and use it in GitHub Desktop.
Save knbknb/7b11673f928f93cfd1ade633dc52d84c to your computer and use it in GitHub Desktop.
personal mini cheat sheet: R time series - xts from csv file, and more xts basics that I tend to forget
# see also:
# https://s3.amazonaws.com/assets.datacamp.com/blog_assets/xts_Cheat_Sheet_R.pdf
# Open csv file using read.zoo
my_tsdata <- read.zoo("my_tsdata.csv", sep = ",", FUN = as.Date, header = TRUE, index.column = 1)
my_tsdata <- as.xts(my_tsdata)
# merge by row : Use rbind - no sorting is done by the xts module
temps_xts <- rbind(temps_1_xts, temps_2_xts)
# merge by column - two xts objects by column - index correctly
flights_temps <- merge(flights_xts, temps_monthly)
# convert periods.
# note param for no Open-High-Low-Close in the input data.
# use first in subperiod
temps_monthly <- to.period(temps_xts, period = "months", OHLC = FALSE, indexAt = "firstof")
## Split-apply-combine with index column magically preserved
# Split _One column_ into separate lists per month
monthly_split <- split(my_xts$mean , f = "months")
mean_of_means <- lapply(monthly_split, FUN = mean) # list-of one-column lists
my_monthly <- as.xts(as.numeric(mean_of_means), order.by = index)
#alternative
my_monthly <- do.call(rbind, mean_of_means) # list of many-column lists
# Fill NAs :
# last observation carried forward
my_locf <- na.locf(my_xts)
# next observation carried backward
my_nocb <- na.locf(my_xts, fromLast=TRUE)
# linear approximation
my_approx <- na.approx(my_xts)
## create lags, merge by column
# Create a one month lag
monthlag <- lag(my_xts$dat, k = 1)
# Create a one year lag of US unemployment
yearlag <- lag(my_xts$dat, k = 12)
# Merge your original data with your new lags
my_lags <- merge(my_xts, monthlag, yearlag)
# lag+merge in one go: diff
my_xts$monthlydiff <- diff(my_xts$dat, lag = 1, differences = 1)
# smoothen, or interpolate smaller differences
my_xts$year_avg <- rollapply(my_xts$dat, width = 12, FUN = mean)
# endpoints and period.apply go together.
# Identify a column of dates
close <- endpoints(my_xts, on = "years")
# Calculate closing average at end each period, (_not_ throughout)
period.apply(my_xts[, "some_column"], close, mean)
# using the index column
weekday <- .indexwday(myxts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment