as.POSIXct/as.POSIXlt 和 strptime 之间的区别,用于将字符向量转换为 POSIXct/POSIXlt [英] Difference between as.POSIXct/as.POSIXlt and strptime for converting character vectors to POSIXct/POSIXlt

查看:57
本文介绍了as.POSIXct/as.POSIXlt 和 strptime 之间的区别,用于将字符向量转换为 POSIXct/POSIXlt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里关注了许多关于如何将字符向量转换为日期时间类的问题.我经常看到两种方法,strptime 和 as.POSIXct/as.POSIXlt 方法.我查看了 2 个函数,但不清楚有什么区别.

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

function (x, tz = "", ...) 
UseMethod("as.POSIXct")
<bytecode: 0x069efeb8>
<environment: namespace:base>

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 似乎稍快一些.那么什么给?为什么会有 2 个类似的功能,或者我错过了它们之间的差异?

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,然后进行从 POSIXltPOSIXct.

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 和 strptime 之间的区别,用于将字符向量转换为 POSIXct/POSIXlt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆