[R] From data frame to time series matrix and plot

Rich Shepard rshepard at appl-ecosys.com
Tue Mar 4 00:39:18 CET 2014


   I'm missing something simple here so a pointer is needed.

   The base data is a data frame, col.riv. That was converted to a matrix:

col.riv.mat <- data.matrix(col.riv)

then to a timeSeries object:

col.riv.ts <- ts(col.riv.mat)
> class(col.riv.ts)
[1] "mts"    "ts"     "matrix"

   I would like to plot (separately) time series for the 'disch' and
'tempMean' columns but have not had success in passing the correct syntax to
the plot() function and having the x-axis labels as human-readable dates
rather than the epoch time (which is what I assume is in the date class).

I've extracted some rows from the base data frame with dput() for use in
working out how to proceed:

structure(list(date = structure(c(16071, 16072, 16073, 16074, 
16075, 16076, 16077, 16078, 16079, 16080, 16081, 16082, 16083, 
16084, 16085, 16086, 16087, 16088, 16089, 16090, 16091, 16092, 
16093, 16094, 16095, 16096, 16097, 16098, 16099, 16100, 16101, 
16102, 16103, 16104, 16105, 16106, 16107, 16108, 16109, 16110, 
16111, 16112, 16113, 16114, 16115, 16116, 16117, 16118, 16119, 
16120, 16121, 16122, 16123, 16124, 16125, 16126, 16127, 16128, 
16129, 16130, 16131), class = "Date"), disch = c(119000L, 125000L, 
129000L, 125000L, 122000L, 155000L, 157000L, 152000L, 156000L, 
156000L, 106000L, 147000L, 123000L, 123000L, 128000L, 150000L, 
135000L, 135000L, 134000L, 144000L, 154000L, 152000L, 139000L, 
147000L, 135000L, 147000L, 131000L, 146000L, 116000L, 111000L, 
124000L, 124000L, 120000L, 119000L, 124000L, 132000L, 152000L, 
138000L, 140000L, 137000L, 133000L, 126000L, 102000L, 82900L, 
133000L, 158000L, 116000L, 145000L, 151000L, 125000L, 130000L, 
116000L, 137000L, 133000L, 129000L, 128000L, 126000L, 135000L, 
136000L, 153000L, 172000L), tempMax = c(4.6, 4.7, 4.6, 4.6, 4.4, 
4.4, 4.4, 4.5, 4.4, 4.5, 4.6, 4.6, 4.6, 4.6, 4.6, 4.4, 4.4, 4.4, 
4.4, 4.4, 4.5, 4.5, 4.5, 4.4, 4.3, 4.3, 4.2, 4.1, 4.1, 4.1, 4.1, 
4, 3.9, 3.8, 3.6, 3.5, 3.2, 2.8, 2.7, 2.6, 2.6, 2.5, 2.6, 2.7, 
2.8, 2.7, 2.4, 3, 3, 2.9, 3.1, 3.2, 3.3, 3.4, 3.4, 3.4, 3.6, 
3.9, 4.1, 4.3, 4.2), tempMin = c(4.5, 4.5, 4.5, 4.4, 4.3, 4.2, 
4.3, 4.4, 4.4, 4.3, 4.5, 4.4, 4.5, 4.5, 4.4, 4.3, 4.3, 4.3, 4.3, 
4.4, 4.4, 4.4, 4.4, 4.3, 4.3, 4.2, 4.1, 4, 4, 4, 4, 3.9, 3.8, 
3.6, 3.5, 3.2, 2.8, 2.7, 2.5, 2.4, 2.4, 2.4, 2.4, 2.5, 2.6, 2.4, 
2.3, 2.4, 2.8, 2.7, 2.8, 2.9, 3.1, 3.2, 3.4, 3.4, 3.4, 3.6, 3.9, 
4.1, 4), tempMean = c(4.5, 4.6, 4.6, 4.5, 4.4, 4.3, 4.4, 4.4, 
4.4, 4.4, 4.5, 4.5, 4.5, 4.5, 4.5, 4.4, 4.3, 4.3, 4.4, 4.4, 4.4, 
4.5, 4.5, 4.4, 4.3, 4.3, 4.2, 4.1, 4, 4, 4, 4, 3.8, 3.7, 3.5, 
3.3, 3, 2.8, 2.6, 2.5, 2.5, 2.5, 2.5, 2.6, 2.7, 2.5, 2.4, 2.7, 
2.9, 2.8, 2.9, 3.1, 3.2, 3.3, 3.4, 3.4, 3.5, 3.7, 4, 4.2, 4.1
)), .Names = c("date", "disch", "tempMax", "tempMin", "tempMean"
), row.names = 732:792, class = "data.frame")

   Where have I erred in this process? And, how do I correctly convert a data
from to a ts class for plotting and analyses?

Rich



More information about the R-help mailing list