Henry
2012-Feb-17 19:56 UTC
[R] convert zoo object to "standard" R object so I can plot and output to csv file
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.
R. Michael Weylandt
2012-Feb-17 20:05 UTC
[R] convert zoo object to "standard" R object so I can plot and output to csv file
?write.zoo Perhaps with sep = "," if you have multiple columns. Michael On Fri, Feb 17, 2012 at 2:56 PM, Henry <hccoles at lbl.gov> wrote:> 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. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Gabor Grothendieck
2012-Feb-18 04:34 UTC
[R] convert zoo object to "standard" R object so I can plot and output to csv file
On Fri, Feb 17, 2012 at 2:56 PM, Henry <hccoles at lbl.gov> wrote:> 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. >If z is a zoo object then plot(z) or library(lattice) xyplot(z) will plot it. See ?plot.zoo and ?xyplot.zoo -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Henry
2012-Feb-23 02:54 UTC
[R] convert zoo object to "standard" R object so I can plot and output to csv file
Gabor,
Thanks very much.
I have all the zoo functions to get 1 minute aggregation and 15 min. means
working and now able to write out to a file/etc.
One question on the 15 min. mean results.
m1 <- times("00:01:00")
g <- seq(trunc(start(z),m1),end(z),by = m1)
z1<-na.approx(z,xout = g)
m15 <- times("00:15:00")
ag15 <- aggregate(z1,trunc(time(z1),m15),mean)
Does the value of ag15 get assigned to the "center" of the time, front
or
rear?
Can this be defined?
It seems aggregate(z1,trunc(time(z1),m15),mean) is getting the mean and
"assigning" it to the first time stamp, not a time stamp assigned to
7.5
minutes from the first 1 minute data point used to get the mean.
Is there an option to "create" a new regular timestamp on 15 periods
but
centered or reporting on 7.5 minute periods?
Thanks.
--
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-tp4398302p4412622.html
Sent from the R help mailing list archive at Nabble.com.
Gabor Grothendieck
2012-Feb-23 04:17 UTC
[R] convert zoo object to "standard" R object so I can plot and output to csv file
On Wed, Feb 22, 2012 at 9:54 PM, Henry <hccoles at lbl.gov> wrote:> Gabor, > Thanks very much. > I have all the zoo functions to get 1 minute aggregation and 15 min. means > working and now able to write out to a file/etc. > > One question on the 15 min. mean results. > > m1 <- times("00:01:00") > g <- seq(trunc(start(z),m1),end(z),by = m1) > z1<-na.approx(z,xout = g) > m15 <- times("00:15:00") > ag15 <- aggregate(z1,trunc(time(z1),m15),mean) > > Does the value of ag15 get assigned to the "center" of the time, front or > rear? > Can this be defined? > It seems aggregate(z1,trunc(time(z1),m15),mean) is getting the mean and > "assigning" it to the first time stamp, not a time stamp assigned to 7.5 > minutes from the first 1 minute data point used to get the mean. > > Is there an option to "create" a new regular timestamp on 15 periods but > centered or reporting on 7.5 minute periods? >The by argument of aggregate.zoo defines the time index (as discussed in ?aggregate.zoo) so time index of ag15 is trunc(time(z1), m15); therefore, to push it ahead by 7.5 minutes just use trunc(time(z1), m15) + as.numeric(m15)/2 in its place. -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Henry
2012-Feb-23 20:06 UTC
[R] convert zoo object to "standard" R object so I can plot and output to csv file
Another simple question - trying to specify xlim in a zoo plot and getting
error
my plot line is
plot(z1,
ylim=c(-100,3000),xlim=c(chron("10/30/2011","00:00:00"),chron("10/30/2011","00:20:00")),type="b",xlab="",ylab="1
Minute Fit",cex.lab=1.3)
Error in substring(paste("0", v$day, sep = ""), first =
nchar(paste(v$day)))
:
invalid substring argument(s)
Most of the complete code pasted below.....
fmt<-"%m/%d/%Y %H:%M:%S" # describe the date/time format in the
file
tail1<-function(x) tail(x,1)
z<-read.zoo("Cooling-1.txt",FUN=as.chron,format=fmt,sep="\t",header=TRUE,aggregate=tail1)
par(oma=c(6,1,4,2)) # set the outside space boundaries
par(mfrow=c(2,1)) # set the number of graphs (rows,cols) going on one page
# plot the original data
par(mar=c(1.1, 5, .9, 0.5))
plot(z, ylim=c(-100,3000),type="b",xlab="",ylab="Raw
Data",col="red",cex.lab=1.3)
grid(nx=NA,ny=NULL,lwd=.5,lty=1,col="red")
# calculate and plot the 1 minute straight line interpolation
m1 <- times("00:01:00")
g <- seq(trunc(start(z),m1),end(z),by = m1)
z1<-na.approx(z,xout = g)
#plot the 1 minute linear interpolation fit
par(mar=c(1.1, 5, .9, 0.5))
#the following plot line generates the error
plot(z1,
ylim=c(-100,3000),xlim=c(chron("10/30/2011","00:00:00"),chron("10/30/2011","00:20:00")),type="b",xlab="",ylab="1
Minute Fit",cex.lab=1.3)
grid(nx=NA,ny=NULL,lwd=.5,lty=1,col="darkgrey")
--
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-tp4398302p4415078.html
Sent from the R help mailing list archive at Nabble.com.
Gabor Grothendieck
2012-Feb-23 20:21 UTC
[R] convert zoo object to "standard" R object so I can plot and output to csv file
On Thu, Feb 23, 2012 at 3:06 PM, Henry <hccoles at lbl.gov> wrote:> Another simple question - trying to specify xlim in a zoo plot and getting > error > my plot line is > plot(z1, > ylim=c(-100,3000),xlim=c(chron("10/30/2011","00:00:00"),chron("10/30/2011","00:20:00")),type="b",xlab="",ylab="1 > Minute Fit",cex.lab=1.3) > Error in substring(paste("0", v$day, sep = ""), first = nchar(paste(v$day))) > : > ?invalid substring argument(s) > > Most of the complete code pasted below.....Most??????????? Read the last two lines of every message to r-help. -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com