我试图导入一个csv文件,该文件的字段Ts包含ISO8601时间戳值(例如,2014-12-01T18:54:22.973+0000)。
我已经看到您可以指定列的类:
kd <- read.csv( "my.csv", colClasses=c( "Ts"="?" ))但是,我找不到如何声明时间戳字段。
问题:如何指定此字段是时间戳?
发布于 2014-12-02 22:39:40
如果要将.csv文件直接读入时间序列对象,则可以使用来自zoo包的函数read.zoo()。这在内部调用read.table() (而不是read.csv),然后转换指定的时间索引列。见?read.zoo和vignette("zoo-read", package = "zoo")。
像你这样的时间戳的一个例子是:
csv <-
"x,y,timestamp
0,1,2014-12-01T18:54:22.973+0000
1,2,2014-12-01T19:43:11.862+0000"
read.zoo(text = csv, sep = ",", header = TRUE, index = "timestamp",
format = "%Y-%m-%dT%H:%M:%OS%z", tz = "GMT")这产生了一个带有zoo时间戳的POSIXct系列:
x y
2014-12-01 18:54:22 0 1
2014-12-01 19:43:11 1 2(当然,如果要从磁盘中读取text = csv文件,而不是从R中读取文本字符串,则必须用类似于file = "my.csv"的东西替换.csv )。
发布于 2014-12-02 20:42:08
不知道有什么方法可以直接在阅读时这样做,但作为一种解决办法(直到更有知识的人回答),您可以在读完后进行转换:
kd <- read.csv("my.csv")
% Assume that the timestamp column in the csv file has the header 'timestamp'
kd$newtimestamp <- strptime(kd$timestamp,format="%FT%H:%M:%OS%z")
% By default this will convert all times to your timezone
% but you can control the conversion through the tx argument e.g. tx='GMT' https://stackoverflow.com/questions/27257777
复制相似问题