问题描述:

I have a data set with millisecond data, and I used the following code to convert it to a date/time:

> 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"

But now i want to transfer them from "2012-06-27 10:59:55.28" to "2012-06-27 10:59:55.3" (i.e. only 1 decimal for millisecond). What should I do to modify it?

网友答案:
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"
相关阅读:
Top