Dear All, I have a series of data in which the first column consist of a combination of date and time, for instance 17 April 2008 at 4.01pm, such data is recorded as: 4/17/2008 16:01 I'd like to seperate it into four different columns which consist of Day, Month,Year and Time, respectively. Could someone please advice me on this mater? Thank you, Fir
How to seperate date and time into different columns?
4 messages · FMH, Brian G. Peterson, Gabor Grothendieck +1 more
FMH wrote:
Dear All, I have a series of data in which the first column consist of a combination of date and time, for instance 17 April 2008 at 4.01pm, such data is recorded as: 4/17/2008 16:01 I'd like to seperate it into four different columns which consist of Day, Month,Year and Time, respectively. Could someone please advice me on this mater?
Use xts.
Specify a POSIXlt index
All of the properties will be available in POSIXlt.
However, you didn't state what you wish to accomplish. It is likely
that xts will be able to subset anything you need without splitting
things up.
Realize that using a real time series class will always perform better
than using a data frame.
Cross-posting is considered rude. If you need more help than the above,
please make your query specific to some problem in finance, to keep the
list focused on its topic (finance), or only reply to r-help.
It would be polite to sign your name too...
Regards,
- Brian
Brian G. Peterson http://braverock.com/brian/ Ph: 773-459-4973 IM: bgpbraverock
Normally one wants to store time indexes as a single column so its likely that this is not what you really want to do. You may wish to explain what your final objective is and why you want to do this. However, if you must there are many ways and here is one The first two lines load chron and set up some input data. Now that we have some input the first line of the solution creates a chron object by passing to chron the portion prior to the space and the portion after the space and appending :00 to the latter. The second line uses month.day.year to get the date components and subtraction of the date to get the times.
library(chron)
x <- c("4/17/2008 16:01", "4/18/2008 20:01")
xc <- chron(sub(" .*", "", x), sub(".* (.*)", "\\1:00", x))
with(month.day.year(xc), data.frame(year, month, day, time = xc - dates(xc)))
year month day time 1 2008 4 17 16:01:00 2 2008 4 18 20:01:00 R News 4/1 has an relevant article.
On Fri, Jan 22, 2010 at 1:09 PM, FMH <kagba2006 at yahoo.com> wrote:
Dear All, I have a series of data in which the first column consist of a combination of date and time, for instance 17 April 2008 at 4.01pm, such data is recorded as: 4/17/2008 16:01 I'd like to seperate it into four different columns which consist of Day, Month,Year and Time, respectively. Could someone please advice me on this mater? Thank you, Fir
______________________________________________ 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.
A note about xts: The time class layer is abstracted from the implementation. This allows for some very time specific manipulation without having to manage the underlying details. Take a look at the xts docs and vignette. Also, the functions .indexmon .indexyear etc correspond to the POSIXlt components. The xts object doesn't use this as a class, but this allows you access as if it did. HTH Jeff Jeffrey A. Ryan jeffrey.ryan at insightalgo.com ia: insight algorithmics www.insightalgo.com On Jan 22, 2010, at 12:18 PM, "Brian G. Peterson"
<brian at braverock.com> wrote:
FMH wrote:
Dear All, I have a series of data in which the first column consist of a combination of date and time, for instance 17 April 2008 at 4.01pm, such data is recorded as: 4/17/2008 16:01 I'd like to seperate it into four different columns which consist of Day, Month,Year and Time, respectively. Could someone please advice me on this mater?
Use xts. Specify a POSIXlt index All of the properties will be available in POSIXlt. However, you didn't state what you wish to accomplish. It is likely that xts will be able to subset anything you need without splitting things up. Realize that using a real time series class will always perform better than using a data frame. Cross-posting is considered rude. If you need more help than the above, please make your query specific to some problem in finance, to keep the list focused on its topic (finance), or only reply to r- help. It would be polite to sign your name too... Regards, - Brian -- Brian G. Peterson http://braverock.com/brian/ Ph: 773-459-4973 IM: bgpbraverock
_______________________________________________ R-SIG-Finance at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.