R语言的日期主要涉及以下函数:
#Date function
Sys.Date()#return current date
class(Sys.Date())
as.Date("2014-03-23")
as.Date("12/31/2010", format="%m/%d/%Y")
#converting a date into a string
format(Sys.Date())
as.character(Sys.Date())
format(Sys.Date(),format="%m/%d/%Y")
#Converting Year, Month, and Day into a Date
#ISOdate(year, month, day)
#as.Date(ISOdate(year, month, day))
ISOdate(2013,10,12)
as.Date(ISOdate(2013,10,12))
#convert number into Date
year=c(2008,2009,2010,2011,2012)
month=c(1,2,1,1,1)
day=c(12,12,14,12,18)
ISOdate(year,month,day)
as.Date(ISOdate(year,month,day))
#as.POSIXlt function
d=as.Date("2013-02-19")
p=as.POSIXlt(d)
p$mday #Day of the month (1–31)
p$mon #Month (0–11)
p$year+1900 #Years since 1900
p$yday #Day of the year (0–365)
p$wday #Day of the week (0–6, 0 = Sunday)
#creating a sequence of Dates
#seq function
Bdate=as.Date("2014-01-01")
Edate=as.Date("2014-02-09")
seq(from=Bdate,to=Edate,by=1)
seq(from=Bdate,by=1,length.out=8) #starting date (from), increment (by), and number of dates (length.out)
seq(from=Bdate,by="month",length.out=12)
seq(from=Bdate,by="2 months",length.out=3)
seq(from=Bdate,by="year",length.out=10)
seq(from=Bdate,by="month",len=4)