将字符串解析为日期对象
R 包含一个 Date 类,它是用 as.Date()
创建的,它采用字符串或字符串向量,如果日期不是 ISO 8601 日期格式 YYYY-MM-DD
,则为 strptime
样式标记的格式化字符串。
as.Date('2016-08-01') # in ISO format, so does not require formatting string
## [1] "2016-08-01"
as.Date('05/23/16', format = '%m/%d/%y')
## [1] "2016-05-23"
as.Date('March 23rd, 2016', '%B %drd, %Y') # add separators and literals to format
## [1] "2016-03-23"
as.Date(' 2016-08-01 foo') # leading whitespace and all trailing characters are ignored
## [1] "2016-08-01"
as.Date(c('2016-01-01', '2016-01-02'))
# [1] "2016-01-01" "2016-01-02"