我有以下一组时间数据,我必须将其转换为12小时格式。
-----------------
814
830
1835
1730
1442
820
1430
930
1550
1725
1615
1010
1319
1755
820
1955
1850
710
-------------------------------------
例如: 1300 =下午1:00
我想用R ..
如果有人告诉我如何使用R
转换这段时间,那将会很有帮助答案 0 :(得分:4)
你可以尝试,
format(strptime(substr(as.POSIXct(sprintf("%04.0f", x),
format="%H%M"), 12, 16), '%H:%M'), '%I:%M %p')
#[1] "08:14 AM" "08:30 AM" "06:35 PM" "05:30 PM" "02:42 PM" "08:20 AM"
数据强>
x <- c(814, 830, 1835, 1730,1442, 820)
答案 1 :(得分:1)
这应该有效:
myMtime #time vector
myMtime <- sprintf("%04d", myMtime)
myStdTime <- as.POSIXct(myMtime, format = "%H%M", origin = "1970-01-01", tz = "UTC")
myStdTime #time as unix standard for time zone UTC
请注意,R会自动添加您转换myMtime
当天的年 - 月 - 日。如果要添加特定日期,请执行以下操作:
myMtime #time vector
date <- "2016-01-01"
myMtime <- sprintf("%04d", myMtime)
myStdTime <- as.POSIXct(paste0(date, " ", myMtime), format = "%Y-%m-%d %H%M", origin = "1970-01-01", tz = "UTC")
myStdTime #time as unix standard for time zone UTC
对于输出格式,请使用format
:
format(myStdTime, format = "%r%p")
#or thing like that:
format(myStdTime, format = "%a %b %d %X %Y %Z")
#for further explanations consider:
?strptime