有条件地向列中添加 12 小时

Conditionally Add 12 hours to column

使用library(chron) 这里是

df <- structure(list(Time = structure(c(0.376736111111111, 0.376666666666667, 
0.376435185185185, 0.376354166666667, 0.376319444444444, 0.376284722222222, 
0.376134259259259, 0.376122685185185, 0.376006944444444, 0.37587962962963
), format = "h:m:s", class = "times"), `am/pm` = c("am", "am", 
"am", "am", "am", "pm", "pm", "pm", "pm", "pm")), row.names = c(NA, 
10L), class = "data.frame")

我在 Chron()“时间”格式(以 12 小时为单位)中有一个时间列和一个 am/pm 列。

我想创建一个 MilitaryTime 列,如果下午,时间增加 12 小时,如果上午,时间增加 0 小时

谢谢!

df$Time24 = df$Time + ifelse(df$`am/pm` == "pm", 0.5, 0)
#        Time am/pm   Time24
# 1  09:02:30    am 09:02:30
# 2  09:02:24    am 09:02:24
# 3  09:02:04    am 09:02:04
# 4  09:01:57    am 09:01:57
# 5  09:01:54    am 09:01:54
# 6  09:01:51    pm 21:01:51
# 7  09:01:38    pm 21:01:38
# 8  09:01:37    pm 21:01:37
# 9  09:01:27    pm 21:01:27
# 10 09:01:16    pm 21:01:16