Dear R users,
I am trying to peel out information on hours from a column which contains information on date and hours.
An example
I can easily gather the year (yyyy-mm-dd) information using strptime, whereas I am stuck separating out hours.
So, I haven't been able to figure out how to create a new column based on hours. Any suggestions on how to do it?
Many Thanks.
(& Happy New Year 2012)
I am trying to peel out information on hours from a column which contains information on date and hours.
An example
Code:
# data
x<-dput(structure(list(date = structure(c(4L, 3L, 1L, 5L, 2L), .Label = c("01/14/92 01:03:30",
"02/01/92 16:56:26", "02/27/92 22:29:56", "02/27/92 23:03:20",
"02/28/92 18:21:03"), class = "factor")), .Names = "date", row.names = c(NA,
-5L), class = "data.frame"))
>x
date
1 02/27/92 23:03:20
2 02/27/92 22:29:56
3 01/14/92 01:03:30
4 02/28/92 18:21:03
5 02/01/92 16:56:26
Code:
x$Year<-strptime(x$date, "%m/%d/%y") # peels out year info
> x
date Year
1 02/27/92 23:03:20 1992-02-27
2 02/27/92 22:29:56 1992-02-27
3 01/14/92 01:03:30 1992-01-14
4 02/28/92 18:21:03 1992-02-28
5 02/01/92 16:56:26 1992-02-01
# trying the same for digging out hours don't seem to work as NAs are returned
x$hours<-strptime(x$date, "%H:%M:%S") # trying to peel out hours
>x
date Year hours
1 02/27/92 23:03:20 1992-02-27 <NA>
2 02/27/92 22:29:56 1992-02-27 <NA>
3 01/14/92 01:03:30 1992-01-14 <NA>
4 02/28/92 18:21:03 1992-02-28 <NA>
5 02/01/92 16:56:26 1992-02-01 <NA>
Many Thanks.
(& Happy New Year 2012)