Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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_Type Conversion_Numeric_Milliseconds - Fatal编程技术网

将字符日期和时间(毫秒)转换为R中的数字

将字符日期和时间(毫秒)转换为R中的数字,r,date,type-conversion,numeric,milliseconds,R,Date,Type Conversion,Numeric,Milliseconds,我有以下时间戳向量: Timestamp <- c("30-09-2016 11:45:00.000", "01-10-2016 06:19:57.860", "01-10-2016 06:20:46.393") 我想根据时间戳绘制权重,但时间戳的模式是“字符”,这意味着x轴没有正确读取。你有什么建议把它转换成数字吗 我尝试了as.Date(时间戳,格式='%d-%m-%Y%H:%m:%OS3')但似乎不起作用。使用as.POSIXct as.POSIXct(Timestamp,form

我有以下时间戳向量:

Timestamp <- c("30-09-2016 11:45:00.000", "01-10-2016 06:19:57.860", "01-10-2016 06:20:46.393")
我想根据时间戳绘制权重,但时间戳的模式是“字符”,这意味着x轴没有正确读取。你有什么建议把它转换成数字吗


我尝试了
as.Date(时间戳,格式='%d-%m-%Y%H:%m:%OS3')
但似乎不起作用。

使用
as.POSIXct

as.POSIXct(Timestamp,format = "%d-%m-%Y  %H:%M:%S.%OS")

正如Erdem所说,使用
As.POSIXct
%OS
持续几秒钟

t1 <- c("30-09-2016 11:45:00.000", "01-10-2016 06:19:57.860", "01-10-2016 06:20:46.393")     
t2 <- as.POSIXct(t1,format = "%d-%m-%Y  %H:%M:%OS")
但毫秒分辨率在内部保持不变

diff(t2)
 Time differences in secs
 [1] 66897.860    48.533
如果要显示/打印通过使用设置的毫秒分辨率

options(digits.secs=3)
t2
 [1] "2016-09-30 11:45:00.000 EDT" "2016-10-01 06:19:57.859 EDT" "2016-10-01 06:20:46.392 EDT"

@J_F需要使用
as.POSIXct
而不是
as.Date
来保存时间信息。谢谢你,但是当我按照你的建议做时,毫秒被删掉了,所以我只得到>y y[1]“2016-09-30 11:45:00 CEST”“2016-10-01 06:19:57 CEST”“2016-10-01 06:20:46 CEST”这里可能会发生两件事:1)我认为秒的格式应该是%OS而不是%S.%OS;2)打印值根据选项(位数.secs)四舍五入
diff(t2)
 Time differences in secs
 [1] 66897.860    48.533
options(digits.secs=3)
t2
 [1] "2016-09-30 11:45:00.000 EDT" "2016-10-01 06:19:57.859 EDT" "2016-10-01 06:20:46.392 EDT"