I'm working to transform some data to a quarterly time xts
series object. First my data wasn't an appropriate time-based object and now as.yearqtr
is behaving in I cannot make sense of.
I want the object df
to be transformed so that I can plot it with plot.xts
, my final goal, but I am stuck as shown below.
df <- structure(list(yrQ = structure(1:7, .Label = c("2016-1", "2016-2",
"2016-3", "2016-4", "2016-5", "2016-6", "2016-7"), class = "factor"),
a = c(4.14, 2.83, 3.71, 4.15, 4.63, 4.91, 5.31), b = c(4.25,
3.5, 3.5, 3.5, 3.5, 3.5, 5)), .Names = c("yrQ", "a", "b"
), row.names = c(NA, 7L), class = "data.frame")
df
# yrQ a b
# 1 2016-1 4.14 4.25
# 2 2016-2 2.83 3.21
# 3 2016-3 3.71 3.21
# 4 2016-4 4.15 3.21
# 5 2016-5 4.63 3.21
# 6 2016-6 4.91 3.21
# 7 2016-7 5.31 5.00
# install.packages(c("xts"), dependencies = TRUE)
library(xts)
xts(df, order.by = df[,1])
# Error in xts(df, order.by = df[, 1]) :
# order.by requires an appropriate time-based object
df$yrQ <- as.yearqtr(df$yrQ)
df
# yrQ a b
# 1 2016 Q1 4.14 4.25
# 2 2016 Q2 2.83 3.21
# 3 2016 Q3 3.71 3.21
# 4 2016 Q4 4.15 3.21
# 5 NA QNA 4.63 3.21
# 6 NA QNA 4.91 3.21
# 7 NA QNA 5.31 5.00