Re: [R] How to combine Date and time in one column
Hi Gabor, Thanks very much for the suggestion. It worked well until the point that i tried to plot the xts object. here is my code and the error message. lines-data.frame(date,time,open,high, low,close) #create a dataframe with all the variables z - read.zoo(lines, header = TRUE, index = list(1, 2), FUN = function(d, t) as.chron(paste(as.Date(chron(d)), t))) #create the zoo object plot(z) # works fine x-as.xts(z) #works fine plot(x) Error in parse.format(format[1]) : unrecognized format %b %d %H:%M any ideas? Thanks! On Mon, Nov 22, 2010 at 7:32 AM, Gabor Grothendieck ggrothendi...@gmail.com wrote: On Mon, Nov 22, 2010 at 2:24 AM, rnick nikos.rachma...@gmail.com wrote: 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? Use read.zoo and then as.xts to convert it to xts. The following shows it for chron date/times. Replace textConnection(Lines) with myfile.dat to read it from that file. You can replace the FUN= part with a conversion to any date/time class supported by xts. Here we show it for chron. In the example below we are assuming that the date format is month/day/year. See R News 4/1. Lines - Date Time O H L C 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 1/2/2005 17:30 1.3556 1.3564 1.35535 1.3563 library(xts) # this also pulls in zoo and its read.zoo library(chron) z - read.zoo(textConnection(Lines), header = TRUE, index = list(1, 2), FUN = function(d, t) as.chron(paste(as.Date(chron(d)), t))) x - as.xts(z) -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.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] How to combine Date and time in one column
On Tue, Nov 23, 2010 at 7:56 PM, Nikos Rachmanis nikos.rachma...@gmail.com wrote: Hi Gabor, Thanks very much for the suggestion. It worked well until the point that i tried to plot the xts object. here is my code and the error message. lines-data.frame(date,time,open,high, low,close) #create a dataframe with all the variables z - read.zoo(lines, header = TRUE, index = list(1, 2), FUN = function(d, t) as.chron(paste(as.Date(chron(d)), t))) #create the zoo object plot(z) # works fine x-as.xts(z) #works fine plot(x) Error in parse.format(format[1]) : unrecognized format %b %d %H:%M any ideas? Thanks! Probably a bug plotting chron objects in xts. Either pot it with zoo or use POSIXct and plot it with xts: plot(z$C) # or z - read.zoo(textConnection(Lines), header = TRUE, index = list(1, 2), FUN = function(...) as.POSIXct(paste(...), format = %m/%d/%Y %H:%M)) x - as.xts(z) plot(x) -- 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.
Re: [R] How to combine Date and time in one column
Thanks Gabor! Work perfectly! This is the code i used. 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)) On Tue, Nov 23, 2010 at 8:07 PM, Gabor Grothendieck ggrothendi...@gmail.com wrote: On Tue, Nov 23, 2010 at 7:56 PM, Nikos Rachmanis nikos.rachma...@gmail.com wrote: Hi Gabor, Thanks very much for the suggestion. It worked well until the point that i tried to plot the xts object. here is my code and the error message. lines-data.frame(date,time,open,high, low,close) #create a dataframe with all the variables z - read.zoo(lines, header = TRUE, index = list(1, 2), FUN = function(d, t) as.chron(paste(as.Date(chron(d)), t))) #create the zoo object plot(z) # works fine x-as.xts(z) #works fine plot(x) Error in parse.format(format[1]) : unrecognized format %b %d %H:%M any ideas? Thanks! Probably a bug plotting chron objects in xts. Either pot it with zoo or use POSIXct and plot it with xts: plot(z$C) # or z - read.zoo(textConnection(Lines), header = TRUE, index = list(1, 2), FUN = function(...) as.POSIXct(paste(...), format = %m/%d/%Y %H:%M)) x - as.xts(z) plot(x) -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.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] How to combine Date and time in one column
On 22.11.2010 08:24, rnick wrote: 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 dat$DateTime - strptime(paste(dat$Date, dat$Time), %d/%m/%Y %H:%M) Uwe Ligges 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 __ 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] How to combine Date and time in one column
On Mon, Nov 22, 2010 at 2:24 AM, rnick nikos.rachma...@gmail.com wrote: 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. Date Time O H L C 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 1/2/2005 17: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? Use read.zoo and then as.xts to convert it to xts. The following shows it for chron date/times. Replace textConnection(Lines) with myfile.dat to read it from that file. You can replace the FUN= part with a conversion to any date/time class supported by xts. Here we show it for chron. In the example below we are assuming that the date format is month/day/year. See R News 4/1. Lines - Date Time O H L C 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 1/2/2005 17:30 1.3556 1.3564 1.35535 1.3563 library(xts) # this also pulls in zoo and its read.zoo library(chron) z - read.zoo(textConnection(Lines), header = TRUE, index = list(1, 2), FUN = function(d, t) as.chron(paste(as.Date(chron(d)), t))) x - as.xts(z) -- 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.
[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.