使用read_delim
包中的readr
函数,我想阅读一个csv文件,其日期格式为2016-12-29 09:00:18.456
,明确说明列类型。
从以下来源(https://stat.ethz.ch/R-manual/R-devel/library/base/html/strptime.html,https://www.rdocumentation.org/packages/readr/versions/1.0.0/topics/parse_datetime),我了解到我应该使用此代码:"%Y-%m-%d %H:%M:%OS"
作为代码的一部分:
a <- read_delim("file.csv", col_names = TRUE, delim = ";",
locale = locale(decimal_mark = ","), col_types = cols(
'Start time' = col_datetime("%Y-%m-%d %H:%M:%OS")))
但是,我收到以下错误:
Warning: * parsing failures. row col expected actual 1 Start time date like %Y-%m-%d %H:%M:%OS 2016-12-29 09:00:18.456
我做错了什么?
编辑:
我已经更新了评论中提到的%H:%M
拼写错误。还尝试了%OS3
。另据我的理解,对于可重现的示例,parse_datetime()将给出相同的结果:
parse_datetime("2016-12-29 09:00:18.456", "%Y-%m-%d H%:M%:%OS")
答案 0 :(得分:3)
十进制标记与秒中的.
不一致。我还必须设置options("digits.secs"=3)
才能使其正常工作
a <- read_delim("file.csv", col_names = TRUE, delim = ";",
locale = locale(decimal_mark = "."),
col_types = cols(`Start time` = col_datetime("%Y-%m-%d %H:%M:%OS")))
答案 1 :(得分:1)
a <- read_delim("file.csv", col_names = TRUE, delim = ",", locale = locale(decimal_mark = "."), col_types = cols( 'Start time' = col_datetime("%Y-%m-%d %H:%M:%OS")))
只需要更改分隔符和小数点。