as.POSIXct / as.POSIXlt和将字符向量转换为POSIXct / POSIXlt的strptime之间的区别 [英] Difference between as.POSIXct/as.POSIXlt and strptime for converting character vectors to POSIXct/POSIXlt
问题描述
我在这里提到了一些问题,询问如何将字符向量转换为datetime类。我经常看到2种方法,strptime和as.POSIXct / as.POSIXlt方法。我看了两个功能,但不清楚有什么区别。
I have followed a number of questions here that asks about how to convert character vectors to datetime classes. I often see 2 methods, the strptime and the as.POSIXct/as.POSIXlt methods. I looked at the 2 functions but am unclear what the difference is.
function (x, format, tz = "")
{
y <- .Internal(strptime(as.character(x), format, tz))
names(y$year) <- names(x)
y
}
<bytecode: 0x045fcea8>
<environment: namespace:base>
as.POSIXct
as.POSIXct
function (x, tz = "", ...)
UseMethod("as.POSIXct")
<bytecode: 0x069efeb8>
<environment: namespace:base>
as.POSIXlt
as.POSIXlt
function (x, tz = "", ...)
UseMethod("as.POSIXlt")
<bytecode: 0x03ac029c>
<environment: namespace:base>
进行微型基准测试以查看是否存在性能差异:
Doing a microbenchmark to see if there are performance differences:
library(microbenchmark)
Dates <- sample(c(dates = format(seq(ISOdate(2010,1,1), by='day', length=365), format='%d-%m-%Y')), 5000, replace = TRUE)
df <- microbenchmark(strptime(Dates, "%d-%m-%Y"), as.POSIXlt(Dates, format = "%d-%m-%Y"), times = 1000)
Unit: milliseconds
expr min lq median uq max
1 as.POSIXlt(Dates, format = "%d-%m-%Y") 32.38596 33.81324 34.78487 35.52183 61.80171
2 strptime(Dates, "%d-%m-%Y") 31.73224 33.22964 34.20407 34.88167 52.12422
strptime似乎稍快一些。那么给了什么?为什么会有两个类似的功能,或者我们错过了他们之间的差异?
strptime seems slightly faster. so what gives? why would there be 2 similar functions or are there differences between them that I missed?
推荐答案
那么这些功能做不同的事情。
Well, the functions do different things.
首先,有两个日期/时间的内部实现: POSIXct
,它存储自UNIX纪元(+一些其他数据)和 POSIXlt
,其存储日,月,年,小时,分钟,秒等的列表。
First, there are two internal implementations of date/time: POSIXct
, which stores seconds since UNIX epoch (+some other data), and POSIXlt
, which stores a list of day, month, year, hour, minute, second, etc.
strptime
是一种直接将各种格式的字符向量转换为 POSIXlt
格式的函数
strptime
is a function to directly convert character vectors (of a variety of formats) to POSIXlt
format.
as.POSIXlt
将各种数据类型转换为 POSIXlt
。它试图聪明,做出明智的事情 - 在人物的情况下,它作为一个包装器 strptime
。
as.POSIXlt
converts a variety of data types to POSIXlt
. It tries to be intelligent and do the sensible thing - in the case of character, it acts as a wrapper to strptime
.
as.POSIXct
将各种数据类型转换为 POSIXct
。它也试图聪明,做一个明智的事情 - 在字符的情况下,它首先运行 strptime
,然后从 POSIXlt
到 POSIXct
。
as.POSIXct
converts a variety of data types to POSIXct
. It also tries to be intelligent and do the sensible thing - in the case of character, it runs strptime
first, then does the conversion from POSIXlt
to POSIXct
.
有意义的是, strptime
更快,因为 strptime
仅处理字符输入,而其他人则尝试从输入类型中确定使用哪种方法。它也应该是一个更安全的,因为交付意外的数据只会给出一个错误,而不是试图做智能的事情,可能不是你想要的。
It makes sense that strptime
is faster, because strptime
only handles character input whilst the others try to determine which method to use from input type. It should also be a bit safer in that being handed unexpected data would just give an error, instead of trying to do the intelligent thing that might not be what you want.
这篇关于as.POSIXct / as.POSIXlt和将字符向量转换为POSIXct / POSIXlt的strptime之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!