English 中文(简体)
将字符矢量转换为 POSIXct/ POSSlt 的 POSIXct/ POSSlt 和 将字符矢量转换为 POSIXct/ POSSlt 之间的差异
原标题:Difference between as.POSIXct/as.POSIXlt and strptime for converting character vectors to POSIXct/POSIXlt

我在这里跟踪了几个问题, 询问如何将字符向量转换为日期类。 我经常看到两种方法, 一种是吸附时间, 另一种是 as. POSIXct/ as. POSIXlt 方法。 我查看了这两个函数, 但不清楚区别是什么 。

strptime

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>

做一个微观基准,以确定是否存在性能差异:

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

运动时间似乎稍快一些。那又有什么意义呢?为什么有两种相似的功能,或者我错过了两种功能之间的差异呢?

最佳回答

功能会做不同的事情

首先,日期/时间有两个内部执行日期/时间: POSIXct ,储存自UNIX时代以来的几秒钟(+某些其他数据),和POSIXlt ,储存每日、月、年、小时、分钟、第二等清单。

strptime 是将字符矢量( 各种格式) 直接转换为 < code> POSIXlt 格式的函数。

as.POSIXlt 将各种数据类型转换为 POSIXlt 。 它试图智能和做明智的事情 - 在字符方面, 它作为包件作用到 strptime

as.POSIXct 将各种数据类型转换为 POSIXct 。它也试图智能和做明智的事情 - 在字符方面,它首先运行strptime ,然后从 POSIXlt 转换为 POSIXct

有道理, strptime 速度更快, 因为 strptime 只处理字符输入, 而其他人则试图从输入类型中确定使用哪种方法。 这也应该更安全一点, 因为被上传的意外数据只会造成错误, 而不是试图做可能不是您想要的明智的事情 。

问题回答

There are two POSIXt types, POSIXct and POSIXlt. "ct" can stand for calendar time, it stores the number of seconds since the origin. "lt", or local time, keeps the date as a list of time attributes (such as "hour" and "mon"). Try these examples:

date.hour=strptime("2011-03-27 01:30:00", "%Y-%m-%d %H:%M:%S")

date=c("26/10/2016")

time=c("19:51:30")

day<-paste(date,"T", time)

day.time1=as.POSIXct(day,format="%d/%m/%Y T %H:%M:%S",tz="Europe/Paris")

day.time1

day.time1$year

day.time2=as.POSIXlt(day,format="%d/%m/%Y T %H:%M:%S",tz="Europe/Paris")

day.time2

day.time2$year




相关问题
Mysql compaire two dates from datetime?

I was try to measure what is faster and what should be the best way to compare two dates, from datetime record in MySql db. There are several approaches how to grab a date from date time, but I was ...

iPhone Date Picker rolls over to 2010 on December 27th?

So I implemented a UIDatepicker in one of my applications for scheduling and autodialing teleconferences... everything is pretty much ready to go except, while testing I noticed that when the date ...

Convert date Python

I have MMDDYY dates, i.e. today is 111609 How do I convert this to 11/16/2009, in Python?

specifying date format when using $form->inputs() in CakePHP

I am wondering if there is a way to specify the date format in the forms created using CakePHP s $form->inputs(); Please note that this is not the individual $form->input() but instead $form->inputs() ...

NSDateFormat, super simple! Where am I screwing up?

Ok, this is really simple, maybe I m a getting a bit burnt out, but seems like it should work, Query XML feed, put out date string, format, display in a cell. The issue is I m a getting a NULL ...

sqlite writing a date into an email

I am exporting a date value from sqlite and placing it into an email. The date appears like this 279498721.322872 I am using Objective C in an Iphone App. Does anyone know how to make this export ...

热门标签