[R] R help!

Petr PIKAL petr.pikal at precheza.cz
Thu May 3 15:06:13 CEST 2012


Hi

I would convert it to propper date format and then you can extract 
anything.

dat<-strptime("12/31/11 23:45", format="%m/%d/%y %H:%M")
as.Date(dat)
[1] "2011-12-31"
format(dat, "%H:%M")
[1] "23:45"

Regards
Petr
 

> 
> Hello there, I was wondering if you could help me with a quick R issue.
> 
> I have a data set where one of the columns has both date and time in
> it, e.g. "12/31/11 23:45" in one cell. I want to use R to split this
> column into two new columns: date and time.
> 
> One of the problems with splitting here is that when the dates go into
> single digits there are no 0's in front of months January-September
> (e.g., January is represented by 1 as opposed to 01), so every entry
> is a different length. Therefore, splitting by the space is the only
> option, I think.
> 
> Here's the coding I've developed thus far:
> 
> z$dt <- z$Date                    #time and date is all under z$Date
> foo <- strsplit(" ", z$dt)         #attempted split based on the space
> 
> And then if that were to work, I would proceed use the coding:
> 
> foo2 <- matrix(unlist(foo), ncol = 2, byrow=TRUE)
> z$Date <- foo[ ,1]
> z$Time <- foo[ ,2]
> 
> However, foo <- strsplit(" ", z$dt) isn't working. Do you know what
> the problem is? If you could respond soon, that would be greatly
> appreciated!
> 
> Thanks so much!
> Alex
> 
> ______________________________________________
> 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.



More information about the R-help mailing list