eabanoz February 2016

R change date to a specific order

Date column in my data set starts with "2015-05-08 00:00:00" and ends with "2015-06-07 00:00:00". I would like to create a new column whose name will be day and order this column between 0-31. "2015-05-08 00:00:00" should be 0 "2015-06-07 00:00:00" should be 31.

any advice and suggestions will be greatly appreciated.

Data sample:

structure(list(Hour = structure(1:100, .Label = c("2015-05-08 00:00:00", 
"2015-05-08 01:00:00", "2015-05-08 02:00:00", "2015-05-08 03:00:00", 
"2015-05-08 04:00:00", "2015-05-08 05:00:00", "2015-05-08 06:00:00", 
"2015-05-08 07:00:00", "2015-05-08 08:00:00", "2015-05-08 09:00:00", 
"2015-05-08 10:00:00", "2015-05-08 11:00:00", "2015-05-08 12:00:00", 
"2015-05-08 13:00:00", "2015-05-08 14:00:00", "2015-05-08 15:00:00", 
"2015-05-08 16:00:00", "2015-05-08 17:00:00", "2015-05-08 18:00:00", 
"2015-05-08 19:00:00", "2015-05-08 20:00:00", "2015-05-08 21:00:00", 
"2015-05-08 22:00:00", "2015-05-08 23:00:00", "2015-05-09 00:00:00", 
"2015-05-09 01:00:00", "2015-05-09 02:00:00", "2015-05-09 03:00:00", 
"2015-05-09 04:00:00", "2015-05-09 05:00:00", "2015-05-09 06:00:00", 
"2015-05-09 07:00:00", "2015-05-09 08:00:00", "2015-05-09 09:00:00", 
"2015-05-09 10:00:00", "2015-05-09 11:00:00", "2015-05-09 12:00:00", 
"2015-05-09 13:00:00", "2015-05-09 14:00:00", "2015-05-09 15:00:00", 
"2015-05-09 16:00:00", "2015-05-09 17:00:00", "2015-05-09 18:00:00", 
"2015-05-09 19:00:00", "2015-05-09 20:00:00", "2015-05-09 21:00:00", 
"2015-05-09 22:00:00", "2015-05-09 23:00:00", "2015-05-10 00:00:00", 
"2015-05-10 01:00:00", "2015-05-10 02:00:00", "2015-05-10 03:00:00", 
"2015-05-10 04:00:00", "2015-05-10 05:00:00", "2015-05-10 06:00:00", 
"2015-05-10 07:00:00", "2015-05-10 08:00:00", "2015-05-10 09:00:00", 
"2015-05-10 10:00:00", "2015-05-10 11:00:00", "2015-05-10 12:00:00", 
"2015-05-10 13:00:00", "2015-05-10 14:00:00", "2015-05-10 15:00:00", 
"2015-05-10 16:00:00", "2015-05-10 17:00:00", "2015-05-10 18:00:00", 
"2015-05-10 19:00:0        

Answers


mtoto February 2016

You could convert your Hour column to dates (dropping hours), then to factor and finally to numeric.

library(dplyr)
df$day <- as.Date(df$Hour) %>% as.factor %>% as.numeric

Post Status

Asked in February 2016
Viewed 3,707 times
Voted 5
Answered 1 times

Search




Leave an answer