Re: [R] Time series: xts/zoo object at annual (yearly) frequency
Maybe one of these are close enough: xts(c(2, 4, 5), yearqtr(1991:1993)) as.xts(ts(c(2, 4, 5), 1991)) of if you want only a plain year as the index then then use zoo, zooreg or ts class: library(zoo) zoo(c(2, 4, 5), 1991:1993) zooreg(c(2, 4, 5), 1991) ts(c(2, 4, 5), 1991) On Fri, Oct 6, 2017 at 2:56 AM, Johnwrote: > Hi, > >I'd like to make a time series at an annual frequency. > >> a<-xts(x=c(2,4,5), order.by=c("1991","1992","1993")) > Error in xts(x = c(2, 4, 5), order.by = c("1991", "1992", "1993")) : > order.by requires an appropriate time-based object >> a<-xts(x=c(2,4,5), order.by=1991:1993) > Error in xts(x = c(2, 4, 5), order.by = 1991:1993) : > order.by requires an appropriate time-based object > > How should I do it? I know that to do for quarterly or monthly time > series, we use as.yearqtr or as.yearmon. What about annual? > >Thanks, > > John > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] Time series: xts/zoo object at annual (yearly) frequency
Hi John, Here's one way to do it: vec <- c(2,4,5) yrs <- seq(from=as.Date("1991-01-01"),by="1 year",length=length(vec)) a <- xts(x=vec, order.by=yrs) HTH, Eric On Fri, Oct 6, 2017 at 9:56 AM, Johnwrote: > Hi, > >I'd like to make a time series at an annual frequency. > > > a<-xts(x=c(2,4,5), order.by=c("1991","1992","1993")) > Error in xts(x = c(2, 4, 5), order.by = c("1991", "1992", "1993")) : > order.by requires an appropriate time-based object > > a<-xts(x=c(2,4,5), order.by=1991:1993) > Error in xts(x = c(2, 4, 5), order.by = 1991:1993) : > order.by requires an appropriate time-based object > > How should I do it? I know that to do for quarterly or monthly time > series, we use as.yearqtr or as.yearmon. What about annual? > >Thanks, > > John > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
[R] Time series: xts/zoo object at annual (yearly) frequency
Hi, I'd like to make a time series at an annual frequency. > a<-xts(x=c(2,4,5), order.by=c("1991","1992","1993")) Error in xts(x = c(2, 4, 5), order.by = c("1991", "1992", "1993")) : order.by requires an appropriate time-based object > a<-xts(x=c(2,4,5), order.by=1991:1993) Error in xts(x = c(2, 4, 5), order.by = 1991:1993) : order.by requires an appropriate time-based object How should I do it? I know that to do for quarterly or monthly time series, we use as.yearqtr or as.yearmon. What about annual? Thanks, John [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] Time and xts
Hi all, Gabor came up with a very nice of code for my request: Lines - DateTimeOpenHighLow Close 1/2/200517:05 1.3546 1.3553 1.3546 1.35495 1/2/200517:10 1.3553 1.3556 1.3549 1.35525 1/2/200517:15 1.3556 1.35565 1.35515 1.3553 1/2/200517:25 1.355 1.3556 1.355 1.3555 2/13/2006 5:201.18895 1.18925 1.18835 1.1885 library(zoo) library(chron) z - read.zoo(textConnection(Lines), header = TRUE, index = list(1, 2), FUN = function(d,t) as.chron(paste(d, t), format = %m/%d/%Y %H:%M)) z$New - z$Open + z$High zz - z[hours(time(z)) = 2 hours(time(z)) = 8] The result is: zz Open High Low Close New (02/13/06 05:20:00) 1.19 1.19 1.19 1.19 2.38 However, my original code needs a for loop. Any ideas on how to identify the z positions where time is between 2:00:00 and 07:55:00. Could i do it with the following structure? Lines- DateTimeOpenHighLow Close 1/2/200517:05 1.3546 1.3553 1.3546 1.35495 1/2/200517:10 1.3553 1.3556 1.3549 1.35525 1/2/200517:15 1.3556 1.35565 1.35515 1.3553 1/2/200517:25 1.355 1.3556 1.355 1.3555 2/13/2006 5:201.18895 1.18925 1.18835 1.1885 library(zoo) library(chron) z- read.zoo(textConnection(Lines), header = TRUE, index = list(1, 2), FUN = function(d,t) as.chron(paste(d, t), format = %m/%d/%Y %H:%M)) n=5 entrytrade-function(k) { new[k]-open[k]+high[k] } for (x in seq(from=1,to=n,by=1)) { while (z[time(x)=2 time(x)=7:55]) { entrytrade(x) } } Based on what i have seen the problem lies in the for loop and more specifically in the while statement. That's the error i get: Error in while (z[time(x) = 2 time(x) = 7:55]) { : argument is of length zero Thanks -- View this message in context: http://r.789695.n4.nabble.com/Time-and-xts-tp3219825p3224058.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Time and xts
Nicos, there are certainly better / faster methods to compare a time range which I simply don't know of, but the condensed code below should do it. Your main problem was that you can't compare time(x) to 7:55, since the latter expression denotes a sequence from 7 to 55 in R. Consequently, such an expression compares time(x) with all integer numbers between 7 and 55: time(z[1]) = 7:55 [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [37] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [49] FALSE length(z[time(z) = 2 time(z) = 7:55]) [1] 0 Your while loop was therefore equivalent to m=vector(numeric,0); while(m) print(1); Best Hugo -- snip -- library(zoo) library(chron) Lines- DateTimeOpenHighLow Close 1/2/200517:05 1.3546 1.3553 1.3546 1.35495 1/2/200517:10 1.3553 1.3556 1.3549 1.35525 1/2/200517:15 1.3556 1.35565 1.35515 1.3553 1/2/200517:25 1.355 1.3556 1.355 1.3555 3/14/2006 5:211.18895 2.18925 1.18835 1.1885 3/14/2006 7:562.18895 3.18925 1.18835 1.1885 2/13/2006 5:201.18895 1.18925 1.18835 1.1885 z- read.zoo(con-textConnection(Lines), header = TRUE, index = list(1, 2), FUN = function(d,t) as.chron(paste(d, t), format = %m/%d/%Y %H:%M)) close(con); entrytrade - function(k) { k$Open+k$High } #Note: A binary and operator () instead a logical () is needed here new - entrytrade( z[hours(time(z)) = 2 ( hours(time(z)) + minutes(time(z))/100 = 7.55 ) ] ); print(new); --- snip --- On Tuesday 18 January 2011 22:36:12 rnick wrote: Hi all, Gabor came up with a very nice of code for my request: Lines - DateTimeOpenHighLow Close 1/2/200517:05 1.3546 1.3553 1.3546 1.35495 1/2/200517:10 1.3553 1.3556 1.3549 1.35525 1/2/200517:15 1.3556 1.35565 1.35515 1.3553 1/2/200517:25 1.355 1.3556 1.355 1.3555 2/13/2006 5:201.18895 1.18925 1.18835 1.1885 library(zoo) library(chron) z - read.zoo(textConnection(Lines), header = TRUE, index = list(1, 2), FUN = function(d,t) as.chron(paste(d, t), format = %m/%d/%Y %H:%M)) z$New - z$Open + z$High zz - z[hours(time(z)) = 2 hours(time(z)) = 8] The result is: zz Open High Low Close New (02/13/06 05:20:00) 1.19 1.19 1.19 1.19 2.38 However, my original code needs a for loop. Any ideas on how to identify the z positions where time is between 2:00:00 and 07:55:00. Could i do it with the following structure? Lines- DateTimeOpenHighLow Close 1/2/200517:05 1.3546 1.3553 1.3546 1.35495 1/2/200517:10 1.3553 1.3556 1.3549 1.35525 1/2/200517:15 1.3556 1.35565 1.35515 1.3553 1/2/200517:25 1.355 1.3556 1.355 1.3555 2/13/2006 5:201.18895 1.18925 1.18835 1.1885 library(zoo) library(chron) z- read.zoo(textConnection(Lines), header = TRUE, index = list(1, 2), FUN = function(d,t) as.chron(paste(d, t), format = %m/%d/%Y %H:%M)) n=5 entrytrade-function(k) { new[k]-open[k]+high[k] } for (x in seq(from=1,to=n,by=1)) { while (z[time(x)=2 time(x)=7:55]) { entrytrade(x) } } Based on what i have seen the problem lies in the for loop and more specifically in the while statement. That's the error i get: Error in while (z[time(x) = 2 time(x) = 7:55]) { : argument is of length zero Thanks __ R-help@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.
[R] Time and xts
Hi all, I have run into a problem and some help would be highly appreciated. I have a .csv with the following columns: DateTimeOpenHighLow Close 1/2/200517:05 1.3546 1.3553 1.3546 1.35495 1/2/200517:10 1.3553 1.3556 1.3549 1.35525 1/2/200517:15 1.3556 1.35565 1.35515 1.3553 1/2/200517:25 1.355 1.3556 1.355 1.3555 …. ….. 2/13/2006 5:201.18895 1.18925 1.18835 1.1885 1) Without using zoo, xts or any other time series object, I am trying to run this code but fails to pass the argument to the function #n is the length of the series for (t in seq(from=10,to=n,by=1)) { while (time[t]=='02:00:00') { entrytrade(t) } } Is this possible to do? If yes, any ideas what I am doing wrong? 2) I have also tried with xts by creating the object like this lines-data.frame(date,time,open,high,low,close) z - read.zoo(lines, header = TRUE, index = list(1, 2), FUN = function(d,t) as.POSIXct(paste(date,time), format = %m/%d/%Y %H:%M)) x-as.xts(z) However, I am unable to call just the time and run the above for loop. any ideas here? Thanks -- View this message in context: http://r.789695.n4.nabble.com/Time-and-xts-tp3219722p3219722.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Time and xts
Assuming your csv file looks like: Date,Time,Open,High,Low,Close 1/2/2005,17:05,1.3546,1.3553,1.3546,1.35495 1/2/2005,17:10,1.3553,1.3556,1.3549,1.35525 1/2/2005,17:15,1.3556,1.3556,1.35515,1.3553 1/2/2005,17:25,1.355,1.3556,1.355,1.3555 You could do something like: aa - read.csv(tmp.csv,header=TRUE,stringsAsFactors=FALSE) aa$DateTime - as.POSIXct(paste(aa$Date,aa$Time),format = %d/%m/%Y %H:%M) This gives you a column of POSIXct dates which might be useful for your comparison. It leaves the Time column as text, so you should be able to compare strings like in the code you presented, or you can extract whatever you want from the dates using as.POSIXlt and extracting the component: names(as.POSIXlt(aa$DateTime[1])) Robert On Sun, Jan 16, 2011 at 2:20 PM, rnick nikos.rachma...@gmail.com wrote: Hi all, I have run into a problem and some help would be highly appreciated. I have a .csv with the following columns: Date Time Open High Low Close 1/2/2005 17:05 1.3546 1.3553 1.3546 1.35495 1/2/2005 17:10 1.3553 1.3556 1.3549 1.35525 1/2/2005 17:15 1.3556 1.35565 1.35515 1.3553 1/2/2005 17:25 1.355 1.3556 1.355 1.3555 …. ….. 2/13/2006 5:20 1.18895 1.18925 1.18835 1.1885 1) Without using zoo, xts or any other time series object, I am trying to run this code but fails to pass the argument to the function #n is the length of the series for (t in seq(from=10,to=n,by=1)) { while (time[t]=='02:00:00') { entrytrade(t) } } Is this possible to do? If yes, any ideas what I am doing wrong? 2) I have also tried with xts by creating the object like this lines-data.frame(date,time,open,high,low,close) z - read.zoo(lines, header = TRUE, index = list(1, 2), FUN = function(d,t) as.POSIXct(paste(date,time), format = %m/%d/%Y %H:%M)) x-as.xts(z) However, I am unable to call just the time and run the above for loop. any ideas here? Thanks -- View this message in context: http://r.789695.n4.nabble.com/Time-and-xts-tp3219722p3219722.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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. __ R-help@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.
Re: [R] Time and xts
On Sat, Jan 15, 2011 at 11:20 PM, rnick nikos.rachma...@gmail.com wrote: Hi all, I have run into a problem and some help would be highly appreciated. I have a .csv with the following columns: Date Time Open High Low Close 1/2/2005 17:05 1.3546 1.3553 1.3546 1.35495 1/2/2005 17:10 1.3553 1.3556 1.3549 1.35525 1/2/2005 17:15 1.3556 1.35565 1.35515 1.3553 1/2/2005 17:25 1.355 1.3556 1.355 1.3555 …. ….. 2/13/2006 5:20 1.18895 1.18925 1.18835 1.1885 1) Without using zoo, xts or any other time series object, I am trying to run this code but fails to pass the argument to the function #n is the length of the series for (t in seq(from=10,to=n,by=1)) { while (time[t]=='02:00:00') { entrytrade(t) } } Is this possible to do? If yes, any ideas what I am doing wrong? Posted code needs to be self contained and reproducible so that others can copy it from your post, paste it into R and see the same problem you are seeing. See last line to every r-help message. 2) I have also tried with xts by creating the object like this lines-data.frame(date,time,open,high,low,close) z - read.zoo(lines, header = TRUE, index = list(1, 2), FUN = function(d,t) as.POSIXct(paste(date,time), format = %m/%d/%Y %H:%M)) x-as.xts(z) However, I am unable to call just the time and run the above for loop. any ideas here? If by calling the time is intended to refer to extracting the time then time(x) gives a vector of times. -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com __ R-help@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.