R语言中使用as.Date想转变日期的形式 as.Date(x,format ("%Y%d%m")

Python015

R语言中使用as.Date想转变日期的形式 as.Date(x,format ("%Y%d%m"),第1张

在我们日常所遇到的数据分析任务中,会遇到很多与日期时间挂钩的数据,比如本月每日的销售额和网页一天内每个时间节点的点击量。这类型的数据大多数为时间序列,而时间序列分析在日常中也是很常见的。现在我们先来聊一下R语言中关于日期时间的处理,之后有时间的话就学习一些有关时间序列分析的方法。

一、日期函数as.Date()函数

R中自带的函数as.Date首先和大家介绍一下它的日常用法,第一个就是我们使用as.Date来返回日期数据形式,且默认的格式为年-月-日,format参数用于识别输入的日期按照那种数据逻辑输入,比如下面数据是以"*年*月*日"的逻辑输入:

>as.Date("2019年9月28日", format = "%Y年%m月%d日")

[1] "2019-09-28"

其中我们看到上面%Y等等的字符,其实是日期格式的一种字符形式,常用的格式如下:

第二个用法就是我们给定起点日期,再输入延后天数,就可以输出对应的日期:

>as.Date(31,origin ='2019-01-01')

[1] "2019-02-01"

二、时间函数POSIXct与POSIXlt

(1).POSIXIt主要特点:作用是打散时间,把时间分成年、月、日、时、分、秒,并进行存储我们可以结合unclass()函数,从而提取日期时间信息。比如:

>unclass(as.POSIXlt('2018-9-7 8:12:23'))

$sec

[1] 23

$min

[1] 12

$hour

[1] 8

$mday

[1] 7

$mon

[1] 8

$year

[1] 118

$wday

[1] 5

$yday

[1] 249

$isdst

[1] 0

$zone

[1] "CST"

$gmtoff

[1] NA

我们输入带时间的日期数据,利用unclass和as.POSIXlt函数就可以返回秒、分、时、日、该年已过月数、已过年数(从1900起)、星期几、该天对应该年的第几天,时区等等。

(2).POSIXct 是以1970年1月1号8点开始的以秒进行存储,如果是负数,则是之前的日期时间;正数则是之后,比如:

>unclass(as.POSIXct('1970-1-1 8:00:20'))

[1] 20

attr(,"tzone")

[1] ""

三、日期时间的运算

(1).日期相减,得到相差的天数

>as.Date("2019-10-01") - as.Date('2019-9-26')

Time difference of 5 days

(2).带时间的日期相减,得到相差数(可以指定units参数为"secs","mins","hours","days")

>difftime('2019-10-1 10:00:00',"2019-10-1 6:00:00",units="hours")

Time difference of 4 hours

udf_async_rbind <- function(path= "D:/R/oper_key_index/data/d_data/",

name= "移网线上单" ,

Date_temp = format(Date_stemp,"%Y%m%d"), # 直接引用变量不执行,需加函数转为常量

Date_last_month_day = ymd("2021-08-31") , # 函数内变量名不要与外部变量名重复

Date_last_month_day1= ymd("2021-07-31") ,

cols=190 ){

x <- udf_DT(paste0(path,name,Date_temp,".xlsx") , 1 ,0)

y <- udf_DT(paste0(path,name,format(Date_last_month_day,"%Y%m%d"),".xlsx"),1,0)

z <- udf_DT(paste0(path,name,format(Date_last_month_day1,"%Y%m%d"),".xlsx"),1,0)

if(ncol(x)>70){

x <- x[,1:cols]

y <- y[,1:cols]

z <- z[,1:cols]}else{print("全列导入")}

setnames(y,names(x))

table <- rbind(x, y)

return(table)

}

mob_dev_2ilist <- udf_async_rbind( name = "移网线上单" )

Sys.Date( ) returns today's date.

date() returns the current date and time.

# print today's date

today <-Sys.Date()

format(today, format="%B %d %Y")

"June 20 2007"

# convert date info in format 'mm/dd/yyyy'

strDates <- c("01/05/1965", "08/16/1975")

dates <- as.Date(strDates, "%m/%d/%Y")

# convert dates to character data

strDates <- as.character(dates)

--------------------------------------

>as.Date('1915-6-16')

[1] "1915-06-16"

>as.Date('1990/02/17')

[1] "1990-02-17"

>as.Date('1/15/2001',format='%m/%d/%Y')

[1] "2001-01-15"

>as.Date('April 26, 2001',format='%B %d, %Y')

[1] "2001-04-26"

>as.Date('22JUN01',format='%d%b%y') # %y is system-specificuse with caution

[1] "2001-06-22"

>bdays = c(tukey=as.Date('1915-06-16'),fisher=as.Date('1890-02-17'),

+ cramer=as.Date('1893-09-25'), kendall=as.Date('1907-09-06'))

>weekdays(bdays)

tukey fisher cramer kendall

"Wednesday""Monday""Monday""Friday"

>dtimes = c("2002-06-09 12:45:40","2003-01-29 09:30:40",

+"2002-09-04 16:45:40","2002-11-13 20:00:40",

+"2002-07-07 17:30:40")

>dtparts = t(as.data.frame(strsplit(dtimes,' ')))

>row.names(dtparts) = NULL

>thetimes = chron(dates=dtparts[,1],times=dtparts[,2],

+ format=c('y-m-d','h:m:s'))

>thetimes

[1] (02-06-09 12:45:40) (03-01-29 09:30:40) (02-09-04 16:45:40)

[4] (02-11-13 20:00:40) (02-07-07 17:30:40)

>dts = c("2005-10-21 18:47:22","2005-12-24 16:39:58",

+ "2005-10-28 07:30:05 PDT")

>as.POSIXlt(dts)

[1] "2005-10-21 18:47:22" "2005-12-24 16:39:58"

[3] "2005-10-28 07:30:05"

>dts = c(1127056501,1104295502,1129233601,1113547501,

+ 1119826801,1132519502,1125298801,1113289201)

>mydates = dts

>class(mydates) = c('POSIXt','POSIXct')

>mydates

[1] "2005-09-18 08:15:01 PDT" "2004-12-28 20:45:02 PST"

[3] "2005-10-13 13:00:01 PDT" "2005-04-14 23:45:01 PDT"

[5] "2005-06-26 16:00:01 PDT" "2005-11-20 12:45:02 PST"

[7] "2005-08-29 00:00:01 PDT" "2005-04-12 00:00:01 PDT"

>mydate = strptime('16/Oct/2005:07:51:00',format='%d/%b/%Y:%H:%M:%S')

[1] "2005-10-16 07:51:00"

>ISOdate(2005,10,21,18,47,22,tz="PDT")

[1] "2005-10-21 18:47:22 PDT"

>thedate = ISOdate(2005,10,21,18,47,22,tz="PDT")

>format(thedate,'%A, %B %d, %Y %H:%M:%S')

[1] "Friday, October 21, 2005 18:47:22"

>mydate = as.POSIXlt('2005-4-19 7:01:00')

>names(mydate)

[1] "sec" "min" "hour" "mday" "mon" "year"

[7] "wday" "yday" "isdst"

>mydate$mday

[1] 19