R中常见日期操作(持续更)
as.POSIXct as.POSIXlt
作用:转换成日历格式日期加上时间(以最接近的秒为准)
可以转化哪些类型:R中将日期转化或者打乱成不同的字符串,然后赋予不同的数据格式进行存储,通过unclass函数去除不同日期存储的格式,可以发现这一规律。
As. POSIXct
以秒存储时间,首先计算目标日期距离1970-01-01 00:00:00以秒为单位的具体,然后存储这个结果。
根据POSIXct存储时间的特点,有一个扩展应用,例:
Z<-145566666 # Z是以秒存储的日期,基础点是1960-01-01,通过as.POSIXct函数转化成标准日历日期+时间的格式。As.POSIXct(z, origin=”1960-01-01”)
Attention:
POSIXct and POSIXlt 函数同样可以转化2001-02-03 or 2001/02/03格式的字符串,但是对于2001/02/03格式的字符串存在歧义问题,先通过striptime函数转化成标准格式。
They can also convert character strings of the formats "2001-02-03"
and "2001/02/03"
optionally followed by white space and a time in the format "14:52"
or "14:52:03"
. (Formats such as "01/02/03"
are ambiguous but can be converted via a format specification by strptime
.) #HELP文档
Striptime函数
function to convert between character and objects of classes “POSIXct” and “POSIXlt” respresenting calander data and time. %Y-%m-%d %H:%M:%S
%m Month of decimal number
%M Minute of decimal number
%Y year with cenury
%d Day of the month in decimal number
%S second as integer
%b abbreviated month name in th current local platform
%B full month name in the current local platform
%a abbreviated weekday name in the current local platform
%A full weekday name in the current local platform
R中自带日期函数as.Data() 将日期转化为天数进行存储,以1970-01-01为基准时间点。
对于不是2011-03-04 or 2011/03/04格式的输入形式应该先用format转化成标准形式。