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
Brian G. Peterson
2010-Jan-22 18:18 UTC
[R] [R-SIG-Finance] How to separate date and time into different columns?
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
Henrique Dallazuanna
2010-Jan-22 18:20 UTC
[R] How to seperate date and time into different columns?
Try this: strDate <- c('4/17/2008 16:01', '4/18/2008 17:13') do.call(rbind, strsplit(strDate, "[[:punct:]]|\\s"))[,c(2, 1, 3:5)] On Fri, Jan 22, 2010 at 4: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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
jim holtman
2010-Jan-22 18:22 UTC
[R] How to seperate date and time into different columns?
> x <- as.POSIXct('4/17/2008 16:01', format='%m/%d/%Y %H:%M') > x[1] "2008-04-17 16:01:00 EDT"> # now create a four element vector of your desired values > y <- strsplit(format(x, "%d %m %Y %H:%M"), ' ') > y[[1]] [1] "17" "04" "2008" "16:01">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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Gabor Grothendieck
2010-Jan-22 18:43 UTC
[R] How to seperate date and time into different columns?
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. >