[R] convert zoo object to "standard" R object so I can plot and output to csv file

Henry hccoles at lbl.gov
Fri Feb 17 20:56:26 CET 2012


Another newbie question
I got the 1 minute spine interpolation and 15 mean aggregation working with
many thanks to Gabor Grothendieck using Zoo functions.  I got a tip from
Hasan Diwan to look at xts but it seemed I would make better progress using
code from Gabor.

Now I'm having trouble plotting this zoo object.  I'm thinking I want a
function to "split" the zoo object back to a regular R object with x time
values and y values so I can plot using plot functions I'm familiar with.

What is the vector name that has the values (e.g. the first value is
432.2189)

I also got the 15 minute aggregation mean working - I'm happy about that
also.

In addition next I'll want to write out a csv. file of the 15min block
aggregated mean data.

Here is the trial data and code I used so far.


Lines <- "10/11/2011 23:30:01     432.22
10/11/2011 23:31:17     432.32
10/11/2011 23:35:00     432.32
10/11/2011 23:36:18     432.22
10/11/2011 23:37:18     432.72
10/11/2011 23:39:19     432.23
10/11/2011 23:40:02     432.23
10/11/2011 23:45:00     432.23
10/11/2011 23:45:20     429.75
10/11/2011 23:46:20     429.65
10/11/2011 23:50:00     429.65
10/11/2011 23:51:22     429.75
10/11/2011 23:55:01     429.75
10/11/2011 23:56:23     429.55
10/12/2011 0:00:07      429.55
10/12/2011 0:01:24      429.95
10/12/2011 0:05:00      429.95
10/12/2011 0:06:25      429.85
10/12/2011 0:10:00      429.85
10/12/2011 0:11:26      428.85
10/12/2011 0:15:00      428.85
10/12/2011 0:20:03      428.85
10/12/2011 0:21:29      428.75
10/12/2011 0:25:01      428.75
10/12/2011 0:30:01      428.75
10/12/2011 0:31:31      428.75"

library(zoo)
library(chron)

fmt <- "%m/%d/%Y %H:%M:%S"
toChron <- function(d, t) as.chron(paste(d, t), format = fmt)

z <- read.zoo(text = Lines, index = 1:2, FUN = toChron)

# 1 minute spline fit
m1 <- times("00:01:00")
g <- seq(trunc(start(z), m1), end(z), by = m1)
na.spline(z, xout = g) # this did what I want but what is the vector name?

# 15 minute aggregates
m15 <- times("00:15:00")
ag15 <- aggregate(z, trunc(time(z), m15), mean)

the results of the na.spline(z, xout = g) function below
(10/11/11 23:30:00) (10/11/11 23:31:00) (10/11/11 23:32:00) (10/11/11
23:33:00) (10/11/11 23:34:00) (10/11/11 23:35:00) 
           432.2189            432.2950            432.3869           
432.4584            432.4545            432.3200 
(10/11/11 23:36:00) (10/11/11 23:37:00) (10/11/11 23:38:00) (10/11/11
23:39:00) (10/11/11 23:40:00) (10/11/11 23:41:00) 
           432.1639            432.5834            432.7443           
432.3624            432.2095            433.8208 
(10/11/11 23:42:00) (10/11/11 23:43:00) (10/11/11 23:44:00) (10/11/11
23:45:00) (10/11/11 23:46:00) (10/11/11 23:47:00) 
           436.3606            438.0969            437.2974           
432.2300            428.9265            430.6503 
(10/11/11 23:48:00) (10/11/11 23:49:00) (10/11/11 23:50:00) (10/11/11
23:51:00) (10/11/11 23:52:00) (10/11/11 23:53:00) 
           430.8493            430.2351            429.6500           
429.6715            429.8502            429.9054 
(10/11/11 23:54:00) (10/11/11 23:55:00) (10/11/11 23:56:00) (10/11/11
23:57:00) (10/11/11 23:58:00) (10/11/11 23:59:00) 
           429.8623            429.7522            429.6074           
429.4636            429.3664            429.3678 
(10/12/11 00:00:00) (10/12/11 00:01:00) (10/12/11 00:02:00) (10/12/11
00:03:00) (10/12/11 00:04:00) (10/12/11 00:05:00) 
           429.5200            429.8310            430.0703           
430.1312            430.0707            429.9500 
(10/12/11 00:06:00) (10/12/11 00:07:00) (10/12/11 00:08:00) (10/12/11
00:09:00) (10/12/11 00:10:00) (10/12/11 00:11:00) 
           429.8495            429.9134            430.0879           
430.1446            429.8500            429.1407 
(10/12/11 00:12:00) (10/12/11 00:13:00) (10/12/11 00:14:00) (10/12/11
00:15:00) (10/12/11 00:16:00) (10/12/11 00:17:00) 
           428.6042            428.4933            428.6367           
428.8500            428.9834            429.0264 
(10/12/11 00:18:00) (10/12/11 00:19:00) (10/12/11 00:20:00) (10/12/11
00:21:00) (10/12/11 00:22:00) (10/12/11 00:23:00) 
           429.0031            428.9376            428.8542           
428.7773            428.7315            428.7217 
(10/12/11 00:24:00) (10/12/11 00:25:00) (10/12/11 00:26:00) (10/12/11
00:27:00) (10/12/11 00:28:00) (10/12/11 00:29:00) 
           428.7330            428.7498            428.7594           
428.7615            428.7588            428.7541 
(10/12/11 00:30:00) (10/12/11 00:31:00) 
           428.7500            428.7491 

the results from when I enter ag15 in the R command line - which looks
correct

(10/11/11 23:30:00) (10/11/11 23:45:00) (10/12/11 00:00:00) (10/12/11
00:15:00) (10/12/11 00:30:00) 
           432.3229            430.0471            429.6667           
428.8000            428.7500 




--
View this message in context: http://r.789695.n4.nabble.com/convert-zoo-object-to-standard-R-object-so-I-can-plot-and-output-to-csv-file-tp4398302p4398302.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list