I'm working through learning tidyverse principles and was wondering if there was an easier/better way to bring in my data that includes a datetime vector in a m/d/Y H:M:S AM/PM format. Currently I import with read_csv
, which recognizes the column in character format, and then I use ludridate to create a new column using mdy_hms
to parse the datetime column:
> test <- read_csv("data.csv")
Parsed with column specification:
cols(
ActivityMinute = col_character(),
Steps = col_integer()
)
> head(test)
# A tibble: 6 x 2
ActivityMinute Steps
<chr> <int>
1 5/12/2016 12:00:00 AM 0
2 5/12/2016 12:01:00 AM 0
3 5/12/2016 12:02:00 AM 0
4 5/12/2016 12:03:00 AM 0
5 5/12/2016 12:04:00 AM 0
6 5/12/2016 12:05:00 AM 0
> test$datetime <- mdy_hms(test$ActivityMinute)
> head(test)
# A tibble: 6 x 3
ActivityMinute Steps datetime
<chr> <int> <dttm>
1 5/12/2016 12:00:00 AM 0 2016-05-12 00:00:00
2 5/12/2016 12:01:00 AM 0 2016-05-12 00:01:00
3 5/12/2016 12:02:00 AM 0 2016-05-12 00:02:00
4 5/12/2016 12:03:00 AM 0 2016-05-12 00:03:00
5 5/12/2016 12:04:00 AM 0 2016-05-12 00:04:00
6 5/12/2016 12:05:00 AM 0 2016-05-12 00:05:00
Is there a better way to do this, perhaps using cols()? I tried specifying the ActivityMinute
as col_datetime
, but that didn't work. Any tips for better code/process are appreciated.