R語言日期的表示和運算(詳細總結)


 

1、取出當前日期

Sys.Date()

[1] "2014-10-29"

date()  #注意:這種方法返回的是字符串類型

[1] "Wed Oct 29 20:36:07 2014"

2、在R中日期實際是double類型,是從1970年1月1日以來的天數

typeof(Sys.Date())

[1] "double"

3、轉換為日期

用as.Date()可以將一個字符串轉換為日期值,默認格式是yyyy-mm-dd。

as.Date("2007-02-01")   #得到"2007-02-01",顯示為字符串,但實際是用double存儲的

as.double(as.Date("1970-01-01"))  #結果為0,是從1970年1月1日以來的天數。

可以把定制的日期字符串轉換為日期型

as.Date("2007年2月1日", "%Y年%m月%d日") 

[1] "2007-02-01"

格式

意義

%Y

年份,以四位數字表示,2007

%m

月份,以數字形式表示,從01到12

%d

月份中當的天數,從01到31

 

 

%b

月份,縮寫,Feb

%B

月份,完整的月份名,指英文,February

%y

年份,以二位數字表示,07

4、把日期值輸出為字符串

today <- Sys.Date()

format(today, "%Y年%m月%d日")

[1] "2014年10月29日"

5、計算日期差

由於日期內部是用double存儲的天數,所以是可以相減的。

today <- Sys.Date()

gtd <- as.Date("2011-07-01")   

today - gtd

Time difference of 1216 days  

 

用difftime()函數可以計算相關的秒數、分鍾數、小時數、天數、周數

difftime(today, gtd, units="weeks")  #還可以是“secs”, “mins”, “hours”, “days”

Time difference of 173.7143 weeks

 

#日期型數據
在R中自帶的日期形式為:as.Date();以數值形式存儲;
對於規則的格式,則不需要用format指定格式;如果輸入的格式不規則,可以通過format指定的格式讀入;
標准格式:
年-月-日或者年/月/日;如果不是以上二種格式,則會提供錯誤;
as.Date('23-2013-1')
錯誤於charTo按照Date(x) : 字符串的格式不夠標准明確
> as.Date('23-2013-1',format='%d-%Y-%m')
[1] "2013-01-23"

格式

意義

%d

月份中當的天數

%m

月份,以數字形式表示

%b

月份,縮寫

%B

月份,完整的月份名,指英文

%y

年份,以二位數字表示

%Y

年份,以四位數字表示

#其它日期相關函數
weekdays()取日期對象所處的周幾;
months()取日期對象的月份;
quarters()取日期對象的季度;
#POSIX類
The POSIXct class stores date/time values as the number of seconds since January 1, 1970, while the POSIXlt class stores them as a list with elements for second, minute, hour, day, month, and year, among others.
POSIXct 是以1970年1月1號開始的以秒進行存儲,如果是負數,則是1970年以前;正數則是1970年以后。
POSIXlt 是以列表的形式存儲:年、月、日、時、分、秒;
mydate = as.POSIXlt(’2005-4-19 7:01:00’)
names(mydate)
默認情況下,日期之前是以/或者-進行分隔,而時間則以:進行分隔;
輸入的標准格式為:日期 時間(日期與時間中間有空隔隔開)
時間的標准格式為:時:分 或者 時:分:秒;
如果輸入的格式不是標准格式,則同樣需要使用strptime函數,利用format來進行指定;
#生成案例數據
Dates <- c("2009-09-28","2010-01-15")
Times <- c( "23:12:55", "10:34:02")
charvec <- timeDate(paste(Dates, Times))
timeDate(charvec)
#取系統的時間
Sys.timeDate()
#一個月的第一天
timeFirstDayInMonth()
#一個月的最后一天
timeLastDayInMonth()
#一周當中第幾天
dayOfWeek()
#一年當中的第幾天
dayOfYear()

 

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-specific; use 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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM