1

I have a dataset with non-standard time data - the Excel file has numbers in a variety of different of different formats as shown below.

enter image description here

Trying to convert it to something usable in R - probably HH:MM AM/PM so I use mutate(H1B.format = format(strptime(H1B, "%I:%M %p"), "%H:%M")).

How would I do this - I tried: separate(H1B, into = c('time', 'ampm'), sep = -2, convert = TRUE) to put AM/PM into a separate column, but still need to figure out how to add colons and zeros as needed.

I'm also fairly new to R, so any help would be great!

zx8754
  • 52,746
  • 12
  • 114
  • 209
Shani
  • 21
  • 1
  • A question is more likely to get an answer if it includes data. Take a look at the [wiki](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) about how to provide data to illustrate your questions. – dipetkov Mar 14 '22 at 23:58

1 Answers1

0

You can use lubridate::parse_date_time to parse times in various formats.

library("lubridate")
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union

times_to_parse <- c(
  "10:30PM", "9:30pm", "12am", "10pm", "6am", "5:30pm",
  "1015PM", "1030pm" 
)
time_formats <- c(
  "%I:%M %p", "%I %p"
)

lubridate::parse_date_time(
  times_to_parse, time_formats
)
#> [1] "0000-01-01 22:30:00 UTC" "0000-01-01 21:30:00 UTC"
#> [3] "0000-01-01 00:00:00 UTC" "0000-01-01 22:00:00 UTC"
#> [5] "0000-01-01 06:00:00 UTC" "0000-01-01 17:30:00 UTC"
#> [7] "0000-01-01 22:15:00 UTC" "0000-01-01 22:30:00 UTC"

Created on 2022-03-15 by the reprex package (v2.0.1)

dipetkov
  • 3,380
  • 1
  • 11
  • 19