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.
[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.
[R] How to combine Date and time in one column
Hello everyone, I am trying to built an xts object and i have run into some problems on the data handling. I would really appreciate if someone could help me with the following: 1) I have a OHLC dataset with Time and date in different columns. How could i combine date and time in one column in order to pass on the new column to xts? I have use cbind and data.frame before but i did not manage to yield any good results as the formating of the file changes. DateTime OH L C 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.3551.3556 1.355 1.3555 1/2/200517:30 1.3556 1.3564 1.35535 1.3563 2) It is not clear to me what is the best way to construct the .xts object? Should i use only the Datetime to index or should i also combine it with the rest of the variables? Thanks in advance, N -- View this message in context: http://r.789695.n4.nabble.com/How-to-combine-Date-and-time-in-one-column-tp3053155p3053155.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]] __ 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] Simple Function
Thanks! It worked fine. -- View this message in context: http://r.789695.n4.nabble.com/Simple-Function-tp3035585p3038017.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] Simple Function
Hi guys, Very new to R and your help would be highly appreciated for the following problem. I am trying to create a simple function which registers values within an array through a for loop. These are the steps I have followed: 1) Declared 2 global matrices 2) Create function mat() with i as an input 3) constructed the for loop 4) called mat(2) The problem is that when i try to get y[4] and f[5] the output is: [1] NA my concern is that i am not addressing any of the following topics: 1) definition of global variable 2) the argument does not go through the for loop 3) the matrices definition is not correct 4) the function do not return any values 4) other.. My code is listed below: y=c(NA) f=c(NA) mat-function(i) { for (k in i:10) { y[k]=k+1 f[k]=y[k-1]/2 } } mat(2) Any thoughts or recommendations would be highly appreciated. Thanks in advance, N -- View this message in context: http://r.789695.n4.nabble.com/Simple-Function-tp3035585p3035585.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] Simple Function
Hi guys, Very new to R and your help would be highly appreciated for the following problem. I am trying to create a simple function which registers values within an array through a for loop. These are the steps I have followed: 1) Declared 2 global matrices 2) Create function mat() with i as an input 3) constructed the for loop 4) called mat(2) The problem is that when i try to get y[4] and f[5] the output is: [1] NA my concern is that i am not addressing any of the following topics: 1) definition of global variable 2) the argument does not go through the for loop 3) the matrices definition is not correct 4) other Please check my code below: y=c(NA) f=c(NA) mat-function(i) { for (k in i:10) { y[k]=k+1 f[k]=y[k-1]/2 } } mat(2) Any thoughts or recommendations would be highly appreciated. Thanks in advance, N -- View this message in context: http://r.789695.n4.nabble.com/Simple-Function-tp3035572p3035572.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]] __ 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.