解析日期/時間
用法
parse_datetime(
x,
format = "",
na = c("", "NA"),
locale = default_locale(),
trim_ws = TRUE
)
parse_date(
x,
format = "",
na = c("", "NA"),
locale = default_locale(),
trim_ws = TRUE
)
parse_time(
x,
format = "",
na = c("", "NA"),
locale = default_locale(),
trim_ws = TRUE
)
col_datetime(format = "")
col_date(format = "")
col_time(format = "")
參數
- x
-
要解析的日期的字符向量。
- format
-
格式規範,如下所述。如果設置為“”,則日期時間將解析為 ISO8601,日期和時間使用
locale()
中指定的日期和時間格式。與
strptime()
不同,格式規範必須匹配完整的字符串。 - na
-
要解釋為缺失值的字符串的字符向量。將此選項設置為
character()
以指示沒有缺失值。 - locale
-
區域設置控製默認值因地而異。默認區域設置為 US-centric(如 R),但您可以使用
locale()
創建自己的區域設置來控製默認時區、編碼、小數標記、大標記和日/月名稱等內容。 - trim_ws
-
在解析每個字段之前是否應該刪除前導和尾隨空格(ASCII 空格和製表符)?
值
POSIXct()
向量,其中 tzone
屬性設置為 tz
。無法解析(或未生成有效日期)的元素將被設置為 NA
,並且一條警告消息將通知您失敗的總數。
格式規範
readr
使用類似於 strptime()
的格式規範。元素分為三種類型:
-
日期部分用"%" 後跟一個字母來指定。例如,"%Y" 匹配 4 位數的年份,"%m" 匹配 2 位數的月份,"%d" 匹配 2 位數的日期。如果不存在,例如僅給出年份,則月份和日期默認為
1
(即 Jan 1 日)。 -
空白是零個或多個空白字符的任何序列。
-
任何其他字符都完全匹配。
parse_datetime()
識別以下格式規範:
-
年份:"%Y"(4 位數字)。 "%y"(2 位數字); 00-69 -> 2000-2069,70-99 -> 1970-1999。
-
月份:"%m"(2 位數字)、"%b"(當前區域設置中的縮寫名稱)、"%B"(當前區域設置中的全名)。
-
日:"%d"(2 位數字)、"%e"(可選前導空格)、"%a"(當前語言環境中的縮寫名稱)。
-
小時:"%H" 或 "%I" 或 "%h",在 AM/PM 中使用 I(而不是 H),如果您的時間表示持續時間超過一天,則使用 h(而不是 H)。
-
分鍾:"%M"
-
秒:"%S"(整數秒)、"%OS"(部分秒)
-
時區:"%Z"(作為名稱,例如"America/Chicago")、"%z"(作為與 UTC 的偏移量,例如"+0800")
-
上午/下午指示器:"%p"。
-
非數字:"%." 跳過一個非數字字符,"%+" 跳過一個或多個非數字字符,"%*" 跳過任意數量的非數字字符。
-
自動解析器:"%AD"使用靈活的YMD解析器進行解析,"%AT"使用靈活的HMS解析器進行解析。
-
自 Unix 紀元以來的時間:"%s" 自 Unix 紀元以來的十進製秒數。
-
快捷鍵:"%D" = "%m/%d/%y"、"%F" = "%Y-%m-%d"、"%R" = "%H:%M"、"%T" = "%H:%M:%S"、"%x" = "%y/%m/%d" .
ISO8601支持
目前,readr 並不支持全部 ISO8601。缺少的函數:
-
周和工作日規格,例如"2013-W05"、"2013-W05-10"。
-
序數日期,例如"2013-095"。
-
使用逗號代替句點作為小數分隔符。
解析器也比 ISO8601 寬鬆一些:
-
日期和時間可以用空格分隔,而不僅僅是 T。
-
大多數正確的規範,例如“2009-05-19 14:”和"200912-01"都有效。
也可以看看
其他解析器:col_skip()
, cols_condense()
, cols()
, parse_factor()
, parse_guess()
, parse_logical()
, parse_number()
, parse_vector()
例子
# Format strings --------------------------------------------------------
parse_datetime("01/02/2010", "%d/%m/%Y")
#> [1] "2010-02-01 UTC"
parse_datetime("01/02/2010", "%m/%d/%Y")
#> [1] "2010-01-02 UTC"
# Handle any separator
parse_datetime("01/02/2010", "%m%.%d%.%Y")
#> [1] "2010-01-02 UTC"
# Dates look the same, but internally they use the number of days since
# 1970-01-01 instead of the number of seconds. This avoids a whole lot
# of troubles related to time zones, so use if you can.
parse_date("01/02/2010", "%d/%m/%Y")
#> [1] "2010-02-01"
parse_date("01/02/2010", "%m/%d/%Y")
#> [1] "2010-01-02"
# You can parse timezones from strings (as listed in OlsonNames())
parse_datetime("2010/01/01 12:00 US/Central", "%Y/%m/%d %H:%M %Z")
#> [1] "2010-01-01 18:00:00 UTC"
# Or from offsets
parse_datetime("2010/01/01 12:00 -0600", "%Y/%m/%d %H:%M %z")
#> [1] "2010-01-01 18:00:00 UTC"
# Use the locale parameter to control the default time zone
# (but note UTC is considerably faster than other options)
parse_datetime("2010/01/01 12:00", "%Y/%m/%d %H:%M",
locale = locale(tz = "US/Central")
)
#> [1] "2010-01-01 12:00:00 CST"
parse_datetime("2010/01/01 12:00", "%Y/%m/%d %H:%M",
locale = locale(tz = "US/Eastern")
)
#> [1] "2010-01-01 12:00:00 EST"
# Unlike strptime, the format specification must match the complete
# string (ignoring leading and trailing whitespace). This avoids common
# errors:
strptime("01/02/2010", "%d/%m/%y")
#> [1] "2020-02-01 UTC"
parse_datetime("01/02/2010", "%d/%m/%y")
#> Warning: 1 parsing failure.
#> row col expected actual
#> 1 -- date like %d/%m/%y 01/02/2010
#> [1] NA
# Failures -------------------------------------------------------------
parse_datetime("01/01/2010", "%d/%m/%Y")
#> [1] "2010-01-01 UTC"
parse_datetime(c("01/ab/2010", "32/01/2010"), "%d/%m/%Y")
#> Warning: 2 parsing failures.
#> row col expected actual
#> 1 -- date like %d/%m/%Y 01/ab/2010
#> 2 -- valid date 32/01/2010
#> [1] NA NA
# Locales --------------------------------------------------------------
# By default, readr expects English date/times, but that's easy to change'
parse_datetime("1 janvier 2015", "%d %B %Y", locale = locale("fr"))
#> [1] "2015-01-01 UTC"
parse_datetime("1 enero 2015", "%d %B %Y", locale = locale("es"))
#> [1] "2015-01-01 UTC"
# ISO8601 --------------------------------------------------------------
# With separators
parse_datetime("1979-10-14")
#> [1] "1979-10-14 UTC"
parse_datetime("1979-10-14T10")
#> [1] "1979-10-14 10:00:00 UTC"
parse_datetime("1979-10-14T10:11")
#> [1] "1979-10-14 10:11:00 UTC"
parse_datetime("1979-10-14T10:11:12")
#> [1] "1979-10-14 10:11:12 UTC"
parse_datetime("1979-10-14T10:11:12.12345")
#> [1] "1979-10-14 10:11:12 UTC"
# Without separators
parse_datetime("19791014")
#> [1] "1979-10-14 UTC"
parse_datetime("19791014T101112")
#> [1] "1979-10-14 10:11:12 UTC"
# Time zones
us_central <- locale(tz = "US/Central")
parse_datetime("1979-10-14T1010", locale = us_central)
#> [1] "1979-10-14 10:10:00 CDT"
parse_datetime("1979-10-14T1010-0500", locale = us_central)
#> [1] "1979-10-14 10:10:00 CDT"
parse_datetime("1979-10-14T1010Z", locale = us_central)
#> [1] "1979-10-14 05:10:00 CDT"
# Your current time zone
parse_datetime("1979-10-14T1010", locale = locale(tz = ""))
#> [1] "1979-10-14 10:10:00 UTC"
相關用法
- R readr parse_number 靈活地解析數字
- R readr parse_vector 解析字符向量。
- R readr parse_guess 使用“最佳”類型進行解析
- R readr parse_factor 解析因子
- R readr parse_atomic 解析邏輯數、整數和實數
- R readr problems 檢索解析問題
- R readr datasource 創建源對象。
- R readr melt_delim 返回分隔文件中每個標記的熔化數據(包括 csv 和 tsv)
- R readr read_rds 讀/寫 RDS 文件。
- R readr read_lines 從文件中讀取/寫入行
- R readr read_fwf 將固定寬度文件讀入 tibble
- R readr read_builtin 從包中讀取內置對象
- R readr Tokenizers 分詞器。
- R readr melt_table 返回空格分隔文件中每個標記的熔化數據
- R readr date_names 創建或檢索日期名稱
- R readr type_convert 重新轉換現有 DataFrame 中的字符列
- R readr locale 創建語言環境
- R readr write_delim 將數據幀寫入分隔文件
- R readr with_edition 暫時更改活動閱讀器版本
- R readr read_delim 將分隔文件(包括 CSV 和 TSV)讀入 tibble
- R readr format_delim 將 DataFrame 轉換為分隔字符串
- R readr edition_get 檢索當前活動版本
- R readr readr_example 獲取 readr 示例的路徑
- R readr melt_fwf 返回固定寬度文件中每個標記的熔化數據
- R readr count_fields 計算文件每一行中的字段數
注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Parse date/times。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。