這些值存儲為具有自定義類的 difftime 向量,並且始終以 "seconds" 作為單位,以實現對數字的穩健強製。支持從時間值進行構造、對各種數據類型進行強製轉換以及格式化。可以用作 DataFrame 中的常規列。
hms()
是一個高級構造函數,它接受秒、分、小時和日分量作為數值向量。
new_hms()
是一個低級構造函數,僅檢查其輸入是否具有正確的基類型 numeric 。
is_hms()
檢查對象是否屬於 hms
類。
as_hms()
是一個泛型,支持轉換以外的轉換。默認方法轉發到 vec_cast()
。
用法
hms(seconds = NULL, minutes = NULL, hours = NULL, days = NULL)
new_hms(x = numeric())
is_hms(x)
as_hms(x, ...)
# S3 method for hms
as.POSIXct(x, ...)
# S3 method for hms
as.POSIXlt(x, ...)
# S3 method for hms
as.character(x, ...)
# S3 method for hms
format(x, ...)
# S3 method for hms
print(x, ...)
細節
對於 hms()
,所有參數必須具有相同的長度或者為 NULL
。奇數組合(例如,僅傳遞 seconds
和 hours
但不傳遞 minutes
)將被拒絕。
對於 POSIXct 和 POSIXlt 類型的參數,as_hms()
不執行時區轉換。根據需要使用lubridate::with_tz()
和lubridate::force_tz()
。
例子
hms(56, 34, 12)
#> 12:34:56
hms()
#> hms()
new_hms(as.numeric(1:3))
#> 00:00:01
#> 00:00:02
#> 00:00:03
# Supports numeric only!
try(new_hms(1:3))
#> Error in new_hms(1:3) : `x` must be a vector with type <double>.
#> Instead, it has type <integer>.
as_hms(1)
#> 00:00:01
as_hms("12:34:56")
#> 12:34:56
as_hms(Sys.time())
#> 16:53:34.607852
as.POSIXct(hms(1))
#> [1] "1970-01-01 00:00:01 UTC"
data.frame(a = hms(1))
#> a
#> 1 00:00:01
d <- data.frame(hours = 1:3)
d$hours <- hms(hours = d$hours)
d
#> hours
#> 1 01:00:00
#> 2 02:00:00
#> 3 03:00:00
相關用法
- R hms parse_hms 解析 hms 值
- R hms round_hms 四舍五入或截斷為秒的倍數
- R haven read_xpt 讀寫 SAS 傳輸文件
- R haven zap_missing 將特殊缺失修改為常規 R 缺失
- R SparkR hint用法及代碼示例
- R haven print_labels 打印帶標簽向量的標簽
- R SparkR hashCode用法及代碼示例
- R haven tagged_na “標記”缺失值
- R haven zap_label Zap 變量標簽
- R haven read_sas 讀取 SAS 文件
- R haven labelled 創建一個標記向量。
- R haven read_dta 讀寫Stata DTA文件
- R SparkR histogram用法及代碼示例
- R haven as_factor 將標記向量轉換為因子
- R haven read_spss 讀取和寫入 SPSS 文件
- R SparkR head用法及代碼示例
- R haven zap_empty 將空字符串轉換為缺失值
- R haven labelled_spss SPSS 的標記向量
- R haven zap_labels Zap值標簽
- R dtrMatrix-class 三角形稠密數值矩陣
- R vcov.gam 從 GAM 擬合中提取參數(估計器)協方差矩陣
- R gam.check 擬合 gam 模型的一些診斷
- R ggplot2 annotation_logticks 注釋:記錄刻度線
- R matrix轉list用法及代碼示例
- R Pixel X 射線像素強度隨時間的變化
注:本文由純淨天空篩選整理自Kirill Müller等大神的英文原創作品 A simple class for storing time-of-day values。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。