user319886
user319886

Reputation:

How can i calculate this dataframe?

Here is a head of data.

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

Answers (2)

TarJae
TarJae

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

Kat
Kat

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

Related Questions