简体   繁体   English

如何在R中将12小时转换为24小时

[英]how to convert 12 hour to 24 hour in r

I split the time from 2018-12-31 11:45:00 AM to 2018-12-31 and 11:45:00 aAM successfully. 我成功地将时间从2018-12-31 11:45:00分为2018-12-31和11:45:00 aAM。

However, I get difficulty that convert "11:45:00 AM" to 24 hours. 但是,我很难将“ 11:45:00 AM”转换为24小时。

I know there are several ways to do that, for example, the most popular way is to use strptime and put format="%I:%M:%S %p . I did that several times and made double checked again and again... but still get N/A in my column. Here is, crimeData is my dataset name, toSplitHrs contains time which is "11:45:00 AM" just like what mentioned: 我知道有几种方法可以做到这一点,例如,最流行的方法是使用strptime并将format="%I:%M:%S %p放入。我做了几次,并一次又一次地进行了双重检查。 ..但仍然在我的栏中获得N / A。这是CrimeData是我的数据集名称,toSplitHrs包含的时间是“ 11:45:00 AM”,就像上面提到的那样:

crimeData$toSplitHrs = strptime(crimeData$SplitHrs, format="%I:%M:%S %p")
Police.Beats    SplitMs SplitHrs year month days hours mins sec toSplitHrs
1           28 2018-12-31 11:45:00 2018    12   31    11   45  00       <NA>
2          177 2018-12-31 11:42:00 2018    12   31    11   42  00       <NA>
3          233 2018-12-31 11:30:00 2018    12   31    11   30  00       <NA>
4           91 2018-12-31 11:30:00 2018    12   31    11   30  00       <NA>
5           73 2018-12-31 11:30:00 2018    12   31    11   30  00       <NA>
6          232 2018-12-31 11:27:00 2018    12   31    11   27  00       <NA>

but still, I got N/A result from that... Also, this dataset contains over 10k observations, I really cannot change them one by one...any suggestions are appreciated! 但是,我仍然从中得到了N / A结果。此外,该数据集包含超过1万个观察值,我真的不能一个一个地更改它们……任何建议都值得赞赏!

You can try the format %r for the time, taking into account the am/pm specification (see ?strptime ): 您可以在考虑到am / pm规范的情况下尝试使用%r格式(请参阅?strptime ):

strptime("2018-12-31 11:45:00 am", format="%F %r")
#[1] "2018-12-31 11:45:00 CET"
strptime("2018-12-31 11:45:00 pm", format="%F %r")
#[1] "2018-12-31 23:45:00 CET"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM