有没有办法将格式为“HH:MM:SS AM/PM”的字符串转换为 R 中的 24 小时格式。
我试过 HMS 但它没有用。
当我使用 strptime 时,它也给了我我不想要的日期。
strptime(c("1:00:29 AM","1:00:29 PM"), "%I:%M:%S %p")
#output "2016-08-07 01:00:29 IST" "2016-08-07 13:00:29 IST"
Run Code Online (Sandbox Code Playgroud)
也许我们可以使用 format
time <- format(strptime(c("1:00:29 AM","1:00:29 PM"), "%I:%M:%S %p"), "%H:%M:%S")
time
#[1] "01:00:29" "13:00:29"
Run Code Online (Sandbox Code Playgroud)
这可以times
使用?times
from转换为类chron
library(chron)
times(time)
#[1] 01:00:29 13:00:29
Run Code Online (Sandbox Code Playgroud)