0

I want to create a sequence of times 00:00 - 12:00 and 12:00 to 00:00 with 10 minutes step. How can I do this in R?

I have tried with:

library(chron)
t <- merge(0:23, seq(0, 50, by = 10))
chron(time = paste(x$x, ':', x$y), format = c(times = "h:m"))

But I have 2 problems:

  1. I get an error running chron(time = paste(x$x, ':', x$y), format = c(times = "h:m")):

Error in convert.times(times., fmt) : format h:m may be incorrect

  1. How can I turn it to standard time with AM/PM? Should I merge it twice:

t <- merge(0:12, seq(0, 50, by = 10))

t_am <- merge(t, "AM")

t_pm <- merge(t, "PM")

Or maybe another way using POSIXt?

SteveS
  • 3,164
  • 2
  • 24
  • 43

1 Answers1

1

We can use seq :

format(seq(as.POSIXct('00:00', format = "%H:%M", tz = "UTC"), 
           as.POSIXct(Sys.Date() + 1), by = '10 mins'), "%I:%M%p")

#[1] "12:00AM" "12:10AM" "12:20AM" "12:30AM" "12:40AM" "12:50AM" "01:00AM ...
#[141] "11:20PM" "11:30PM" "11:40PM" "11:50PM" "12:00AM"

Make sure you have the correct locale or set it via :

Sys.setlocale("LC_TIME", "en_US.UTF-8")!
Ronak Shah
  • 355,584
  • 18
  • 123
  • 178
  • But where is the AM/PM strings? @ronak-shah – SteveS Apr 15 '20 at 10:12
  • What do you mean? I see `AM`/`PM` strings in the output. – Ronak Shah Apr 15 '20 at 10:17
  • Here is what I get: [1] "12:00" "12:10" "12:20" "12:30" "12:40" "12:50" "01:00" "01:10" "01:20" "01:30" "01:40" "01:50" "02:00" "02:10" – SteveS Apr 15 '20 at 10:18
  • platform x86_64-pc-linux-gnu status major 3 minor 6.3 year 2020 month 02 day 29 svn rev 77875 language R version.string R version 3.6.3 (2020-02-29) – SteveS Apr 15 '20 at 10:21
  • do you have any idea why do I get without AM/PM? And please correct me if I am wrong - you added ```Sys.Date() + 1``` because it's the next day? @ronak-shah – SteveS Apr 15 '20 at 10:27
  • I am not sure why it doesn't work for you. Yes, we create sequence of 10 minute intervals from today to tomorrow and using `format` we print those datetime in `"%I:%M%p"` format. `%I` is what prints `AM`/`PM`. You can check that in `?strptime` – Ronak Shah Apr 15 '20 at 10:32
  • Ok, so how can I solve it and force it to print AM PM? @ronak-shah – SteveS Apr 15 '20 at 10:33
  • Maybe you have different version of R? I am running your code and get different results...weird – SteveS Apr 15 '20 at 10:37
  • 1
    What is your locale? Is it English? If not, then try switching to English and check. – Ronak Shah Apr 15 '20 at 10:40
  • My locale is: "LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=en_IL.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=en_IL.UTF-8;LC_MESSAGES=en_US.UTF-8;LC_PAPER=en_IL.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_IL.UTF-8;LC_IDENTIFICATION=C" Should I change it? @ronak-shah? – SteveS Apr 15 '20 at 10:48
  • Yes, please change it according to https://stackoverflow.com/questions/16347731/how-to-change-the-locale-of-r . Also, can you try to use `"%r"` instead of `"%I:%M%p"` and see if it works? – Ronak Shah Apr 15 '20 at 10:51
  • Here is what I get: "04:30:00 " "04:40:00 " "04:50:00 " "05:00:00 " "05:10:00 " "05:20:00 " "05:30:00 " "05:40:00 " "05:50:00 " @ronak-shah with %r. I will try changing the locale maybe this is the solution – SteveS Apr 15 '20 at 10:53
  • 1
    For me both `"%r"` and `"%I:%M%p"` work flawlessly. They give output as `"12:00:00 AM" "12:10:00 AM"` and `"12:00AM" "12:10AM"` respectively. – Ronak Shah Apr 15 '20 at 10:55
  • Changed the locale to English without luck: "12:00" "12:10" "12:20" "12:30" "12:40" "12:50" "01:00" "01:10" "01:20" "01:30" "01:40" "01:50" "02:00" "02:10" ```Sys.setlocale("LC_TIME", "English")``` @ronak-shah – SteveS Apr 15 '20 at 10:55