将第二个数据转换为数字

时间:2012-07-12 18:29:54

标签: r date milliseconds

我有一个包含毫秒数据的数据集,我使用以下代码将其转换为日期/时间:

> cl1$date <- strptime(cl1[,1], "%Y-%m-%d %H:%M:%OS")
> head(cl1$date)

[1] "2012-06-06 10:30:00.4" "2012-06-06 10:30:00.5" "2012-06-06 10:30:00.6"
[4] "2012-06-06 10:30:00.7" "2012-06-06 10:30:00.8" "2012-06-06 10:30:00.9"
...    
[71935] "2012-06-27 10:59:55.28" "2012-06-27 10:59:55.38" "2012-06-27 10:59:55.48"
[71938] "2012-06-27 10:59:55.58" "2012-06-27 10:59:55.68" "2012-06-27 10:59:55.78"

但现在我想将它们从"2012-06-27 10:59:55.28"转移到"2012-06-27 10:59:55.3"(即毫秒只有1位小数)。我该怎么做才能修改它?

1 个答案:

答案 0 :(得分:2)

ttt<-as.POSIXlt("2012-06-27 10:59:55.28")

options(digits.secs=1)
ttt
[1] "2012-06-27 10:59:55.2"

options(digits.secs=2)
ttt
[1] "2012-06-27 10:59:55.28"

ttt$sec<-round(ttt$sec,1)
ttt
[1] "2012-06-27 10:59:55.3"

as.character(ttt)
[1] "2012-06-27 10:59:55.3"