Reputation:
I wand to combine the date row and time row, then convert them to date/time format. I've had a struggling with data/time part in r. So could you help me?
Upvotes: 1
Views: 23
Reputation: 78947
You could use unite
to combine and then lubridate
package to get dattime format
library(tidyr)
library(lubridate)
df %>%
unite("datetime", date:time, sep = " ") %>%
mutate(datetime = mdy_hm(datetime))
Example:
df <- tribble(
~date, ~time,
"9/18/2006", 11:12,
"9/18/2006", 14:00,
"9/19/2006", 9:26
)
Output:
datetime
<dttm>
1 2006-09-18 11:12:00
2 2006-09-18 14:00:00
3 2006-09-19 09:26:00
Upvotes: 1
Reputation: 18714
You can concatenate, then parse the fields with the lubridate
package. You didn't name your date frame, so I've called it df
here.
library(lubridate)
df %>% mutate(datetime = paste(date, " ", time) %>% mdy_hm())
Upvotes: 0