Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将第二个数据转换为数字_R_Date_Milliseconds - Fatal编程技术网

R 将第二个数据转换为数字

R 将第二个数据转换为数字,r,date,milliseconds,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-0

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

> 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"
>cl1$date头(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个小数点)。我应该如何修改它?

tttHi,谢谢,这非常有用。我还试着只使用as.POSIXlt,它可以工作,我只是感到困惑,这是否意味着strtime在这里不起作用?或者它们之间有什么区别?
strtime
应该可以工作。但是,只有在需要指定非标准日期时间格式时才需要它。对于标准格式
as.POSIXlt
就足够了。
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"