Reputation: 57
I have one variable called Date
which is of the format 03/08/2015 09:00:00 AM
.
I want to split the column into two columns, namely Date
and Time
. I used colsplit()
command as:
> colsplit(crime$Date,"",names = c("Date","Time"))[1:5,]
Date Time
1 0 3/18/2015 07:44:00 PM
2 0 3/18/2015 11:00:00 PM
3 0 3/18/2015 10:45:00 PM
4 0 3/18/2015 10:30:00 PM
5 0 3/18/2015 09:00:00 PM
But it's not quite as expected. The Date
variable has 0 and the Time
variable has the other values. How do I rectify this?
Also, when I try to include these variables in the crime data set, the column names are date.date
and date.time
.
Upvotes: 0
Views: 322
Reputation: 15072
You can also use tidyr::separate
to divide up character columns.
data %>% separate(date_time, c("date", "time"), sep = " ", remove = TRUE)
Upvotes: 1
Reputation: 1321
For your purposes, just use substr:
data.frame(Date = substr(crime$Date, 1, 10), Time = substr(crime$Date, 12, 19))
But I have to agree with the first comment about not changing this to two columns.
Upvotes: 0