Convert time string with both am/pm and 24 hour format in R

huangapple go评论51阅读模式
英文:

Convert time string with both am/pm and 24 hour format in R

问题

我有一个包含在我的df列中的时间列表,如下所示:

times <- c("11:30am", "3:15pm", "2:25pm", "12:05pm", "8:25pm", "7:52pm", "13:22", "18:45", "21:14", "11:20")

我该如何将这些时间转换为正确的格式?

我希望将带有上午/下午标记的时间去除标记并转换为24小时制,对于那些已经处于正确格式的时间,将其转换为时间格式。

期望的输出:

11:30
15:15
14:25
12:05
20:25
19:52
13:22
18:45
21:14
11:20

我尝试过以下代码:

times <- ifelse(grepl('am', times) | grepl('pm', times),
                  as.POSIXct(times, format = "%I:%M%p"), as.POSIXct(times, format="%H:%M"))

但它只给我返回了一组数字。

英文:

I have a list of times in my df column like so:

times &lt;- c(&quot;11:30am&quot;, &quot;3:15pm&quot;, &quot;2:25pm&quot;, &quot;12:05pm&quot;, &quot;8:25pm&quot;, &quot;7:52pm&quot;, &quot;13:22&quot;, &quot;18:45&quot;, &quot;21:14&quot;, &quot;11:20&quot;)

How can I convert the times to time format so they are all formatted correctly?

I would like the am/pm to be removed from those that have it and converted to 24 hr time, and for those that are already in the correct format, to be converted to time format.
Desired output:

11:30
15:15
14:25
12:05
20:25
19:52
13:22
18:45
21:14
11:20

I tried:

times &lt;- ifelse(grepl(&#39;am&#39;, times) | grepl(&#39;pm&#39;, times),
                  as.POSIXct(times, format = &quot;%I:%M%p&quot;), as.POSIXct(times,format=&quot;%H:%M&quot;))

But it just gave me a list of numbers.

答案1

得分: 5

你可以使用正则表达式来检测输入值的格式。

然后根据小时输入的格式来调整strptime的参数。

ifelse(grepl("am|pm", times),
       format(strptime(times, "%I:%M %p"), format="%H:%M"),
       format(strptime(times, "%H:%M"), format="%H:%M"))
[1] "11:30" "15:15" "14:25" "12:05" "20:25" "19:52" "13:22" "18:45" "21:14" "11:20"
英文:

You can use a regular expression to detect the format of the input value.

Then adjust the argument of strptime depending on the hour input format.

ifelse(grepl(&quot;am|pm&quot;, times),
       format(strptime(times, &quot;%I:%M %p&quot;), format=&quot;%H:%M&quot;),
       format(strptime(times, &quot;%H:%M&quot;), format=&quot;%H:%M&quot;))
[1] &quot;11:30&quot; &quot;15:15&quot; &quot;14:25&quot; &quot;12:05&quot; &quot;20:25&quot; &quot;19:52&quot; &quot;13:22&quot; &quot;18:45&quot; &quot;21:14&quot; &quot;11:20&quot;

答案2

得分: 2

你可以这样做:

sapply(strsplit(times, ":"), function(x) {
  h <- as.numeric(x[1]) + grepl("pm", x[2]) * 12
  if (h == 24) h <- 12
  if (h == 12 & grepl("am|pm", x[2])) h <- 0
  paste(sprintf("%02d", h), 
        gsub("^(\\d+).*$", "\", x[2]), sep = ":")
})
#>  [1] "11:30" "15:15" "14:25" "00:05"  "20:25" "19:52" "13:22" "18:45"
#>  [9] "21:14" "11:20"
英文:

You could do:

sapply(strsplit(times, &quot;:&quot;), function(x) {
  h &lt;- as.numeric(x[1]) + grepl(&quot;pm&quot;, x[2]) * 12
  if(h == 24) h &lt;- 12
  if(h == 12 &amp; grepl(&quot;am|pm&quot;, x[2])) h &lt;- 0
  paste(sprintf(&quot;%02d&quot;, h), 
        gsub(&quot;^(\\d+).*$&quot;, &quot;\&quot;, x[2]), sep = &quot;:&quot;)
})
#&gt;  [1] &quot;11:30&quot; &quot;15:15&quot; &quot;14:25&quot; &quot;00:05&quot;  &quot;20:25&quot; &quot;19:52&quot; &quot;13:22&quot; &quot;18:45&quot;
#&gt;  [9] &quot;21:14&quot; &quot;11:20&quot;

答案3

得分: 0

使用 parse_date

library(parsedate)
format(parse_date(times), "%H:%M")

输出

[1] "11:30" "15:15" "14:25" "12:05" "20:25" "19:52" "13:22" "18:45" "21:14" "11:20"
英文:

Using parse_date

library(parsedate)
format(parse_date(times), &quot;%H:%M&quot;)

-output

[1] &quot;11:30&quot; &quot;15:15&quot; &quot;14:25&quot; &quot;12:05&quot; &quot;20:25&quot; &quot;19:52&quot; &quot;13:22&quot; &quot;18:45&quot; &quot;21:14&quot; &quot;11:20&quot;

huangapple
  • 本文由 发表于 2023年2月24日 06:48:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551097.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定