我如何使用strptime或任何其他函数来解析R中的毫秒时间戳?
time[1]
# [1] "2010-01-15 13:55:23.975"
strptime(time[1], format="%Y-%m-%d %H:%M:%S.%f")
# [1] NA
strptime(time[1], format="%Y-%m-%d %H:%M:%S")
# [1] "2010-01-15 13:55:23"`
Run Code Online (Sandbox Code Playgroud) 我用R导入csv文件时遇到问题:
要导入的示例行:
2010-07-27;91
2010-07-26;93
2010-07-23;88
Run Code Online (Sandbox Code Playgroud)
我使用声明:
data <- read.csv2(file="...", sep=";", dec=".", header=FALSE)
Run Code Online (Sandbox Code Playgroud)
当我尝试将这些数据与使用统计分析产生的其他数据进行汇总时cbind,日期显示为整数,因为它是作为因子导入的.
如果我尝试将其显示为字符串使用as.character,则数字数据也会转换为字符,因此它们无法用于统计过程.
考虑这个 R 代码,它使用定义的时间格式字符串(下面的 timeFormat 变量)来格式化和解析日期:
time = as.POSIXct(1433867059, origin = "1970-01-01")
print(time)
print( as.numeric(time) )
timeFormat = "%Y-%m-%d %H:%M:%OS3"
tz = "EST"
timestamp = format(time, format = timeFormat, tz = tz)
print(timestamp)
timeParsed = as.POSIXct(timestamp, format = timeFormat, tz = tz)
print(timeParsed)
print( as.numeric(timeParsed) )
Run Code Online (Sandbox Code Playgroud)
如果我将其粘贴到运行最新 (3.2.0) 稳定版本的 Windows 机器上的 Rgui 中,我会得到以下信息:
> time = as.POSIXct(1433867059, origin = "1970-01-01")
> print(time)
[1] "2015-06-09 12:24:19 EDT"
> print( as.numeric(time) )
[1] 1433867059
>
> timeFormat = "%Y-%m-%d %H:%M:%OS3"
> tz = …Run Code Online (Sandbox Code Playgroud)