使用 lubridate 在 R 中處理日期和時間


日期時間乍一看似乎很簡單,因為我們在日常生活中都會處理它們。但是,當我們在 R 中處理日期和時間物件時,會涉及很多複雜性。

本文重點介紹如何使用 R 中的lubridate包處理日期和時間。您可以使用以下命令在 CRAN 的終端中本地安裝此包:

install.packages("lubridate")

R 中的資料/時間物件型別

有三種類型的資料/時間物件,如下所示:

  • 日期(<data>) 物件 - 列印日期。

  • 時間(<time>) 物件 - 列印時間。

  • 日期時間物件(<dttm>) - 它是日期和時間的組合。

示例

R 為我們提供了today()函式,我們可以用它來獲取當前日期。例如,

# Include library library("lubridate") # Print the current date print(today())

輸出

[1] "2022-12-13"

正如您在輸出中看到的,當前日期已顯示。

示例

R 還為我們提供了 now() 函式,它列印一個日期時間物件。讓我們考慮以下示例,

# Print the current date-time object print(now())

輸出

[1] "2022-12-13 16:25:31 IST"

正如您在輸出中看到的,當前日期和時間已顯示。

使用字串建立日期

lubridate庫為我們提供了輔助函式,我們可以用它們輕鬆地建立日期物件。這些函式在傳遞字串中元件 (DD、MM、YY) 的排列方面有所不同。讓我們逐一討論這些功能:

使用 ymd() 函式

此函式接受字串作為輸入,但請注意,傳遞字串的元件必須採用以下格式:YY MM DD。

示例

讓我們考慮以下說明此函式工作原理的程式:

library("lubridate") # Print the date in YY-MM-DD format # Passed as YY-MM-DD in the string ymd("2022-01-31")

輸出

[1] "2022-01-31"

正如您在輸出中看到的,日期已以 YY-MM-DD 格式列印。

使用 mdy() 函式

此函式接受字串作為輸入,但請注意,傳遞字串的元件必須採用以下格式:MM-DD-YY

示例

library("lubridate") # Print the date in YY-MM-DD format # Passed as MM-DD-YY in the string mdy("01-31-2022")

輸出

[1] "2022-01-31"

正如您在輸出中看到的,日期已以 YY- MM-DD 格式列印。

使用 dmy() 函式

此函式接受字串作為輸入,但請注意,傳遞字串的元件必須採用以下格式:DD MM YY。

示例

library("lubridate") # Print the date in YY-MM-DD format # Passed as DD-MM-YY in the string dmy("31-01-2022")

輸出

[1] "2022-01-31"

正如您在輸出中看到的,日期已以 YY-MM-DD 格式列印。

使用字串建立日期時間

lubridate 庫還為我們提供了其他輔助函式,我們可以用它們輕鬆地建立日期時間物件。這些函式在傳遞字串中元件 (DD、MM、YY 和 HH、MM、SS) 的排列方面有所不同。讓我們逐一討論這些功能:

ymd_hms(date_time_string)

此函式接受字串作為輸入,但請注意,傳遞字串的元件必須採用以下格式:YY-MM-DD HH:MM:SS。

示例

讓我們考慮以下說明此函式工作原理的程式:

# Include library library("lubridate") # Print the date-time in YY-MM-DD HH:MM:SS # Passed as YY-MM-DD HH:MM:SS in the string ymd_hms("2022-01-31 22:10:10")

輸出

[1] "2022-01-31 22:10:10 UTC"

正如您在輸出中看到的,日期已以 YY-MM-DD HH:MM:SS 格式列印。

ymd_hm(date_time_string)

此函式接受字串作為輸入,但請注意,傳遞字串的元件必須採用以下格式:YY-MM-DD HH:MM。

示例

讓我們考慮以下說明此函式工作原理的程式:

# Include library library("lubridate") # Print the date-time in YY-MM-DD HH:MM # Passed as YY-MM-DD HH:MM in the string ymd_hm("2022-01-31 22:10")

輸出

[1] "2022-01-31 22:10:00 UTC"

正如您在輸出中看到的,日期已以 YY-MM-DD HH:MM 格式列印。

mdy_hms(date_time_string)

此函式接受字串作為輸入,但請注意,傳遞字串的元件必須採用以下格式:MM DD YY

示例

# Include library library("lubridate") # Print the date-time in YY-MM-DD HH:MM:SS # Passed as YY-MM-DD HH:MM:SS in the string mdy_hms("01-31-2022 22:10:10")

輸出

[1] "2022-01-31 22:10:10 UTC"

正如您在輸出中看到的,日期已以 MM-DD-YY HH:MM:SS 格式列印。

mdy_hm(date_time_string)

此函式接受字串作為輸入,但請注意,傳遞字串的元件必須採用以下格式:MM-DD-YY HH:MM

示例

# Include library library("lubridate") # Print the date-time in YY-MM-DD HH:MM # Passed as YY-MM-DD HH:MM in the string mdy_hm("01-31-2022 22:10")

輸出

[1] "2022-01-31 22:10:00 UTC"

正如您在輸出中看到的,日期已以 MM-DD-YY HH:MM 格式列印。

dmy_hms(date_time_string)

此函式接受字串作為輸入,但請注意,傳遞字串的元件必須採用以下格式:DD-MM-YY HH:MM:SS。

示例

# Include library library("lubridate") # Print the date-time in DD-MM-YY HH:MM:SS # Passed as DD-MM-YY HH:MM:SS in the string dmy_hms("31-01-2022 22:10:10")

輸出

[1] "2022-01-31 22:10:10 UTC"

正如您在輸出中看到的,日期已以 DD-MM-YY HH:MM:SS 格式列印。

dmy_hm(date_time_string)

此函式接受字串作為輸入,但請注意,傳遞字串的元件必須採用以下格式:DD-MM-YY HH:MM:SS。

示例

# Include library library("lubridate") # Print the date-time in DD-MM-YY HH:MM # Passed as DD-MM-YY HH:MM in the string dmy_hms("31-01-2022 22:10")

輸出

[1] "2020-01-31 22:22:10 UTC"

正如您在輸出中看到的,日期已以 DD-MM-YY HH:MM 格式列印。

獲取資料/時間的各個元件

lubridate為我們提供了函式,我們可以用它們從日期物件中提取特定元件。例如,月份、日期、年份等。以下函式用於提取特定元件:

函式

描述

year()

從日期或日期時間物件中提取年份

month()

從日期或日期時間物件中提取月份

mday()

從日期或日期時間物件中提取月份中的日期

yday()

從資料或日期時間物件中提取一年中的日期

wday()

從資料或日期時間物件中提取一週中的日期

示例

現在讓我們看看這些函式的工作原理:

# Include library library("lubridate") # Create a date-time object in DD-MM-YY HH:MM:SS # From a string passed in the format: DD-MM-YY HH:MM:SS dateTime <- ymd_hms("2022-01-31 22:10:10") # Print the year print(paste("The year is equal to", year(dateTime)))

輸出

[1] "The day of the year is equal to 31"

列印月份:

print(paste("The month is equal to", month(dateTime)))

輸出

[1] "The month is equal to 1"

列印月份中的日期:

print(paste("The day of the month is equal to", mday(dateTime)))

輸出

[1] "The day of the month is equal to 31"

列印一年中的日期:

print(paste("The day of the year is equal to", yday(dateTime)))

輸出

[1] "The day of the year is equal to 31"

列印一年中的日期:

print(paste("The day of the year is equal to", yday(dateTime)))

輸出

[1] "The day of the year is equal to 31"

列印一週中的日期

print(paste("The weekday is equal to", wday(dateTime)))

輸出

[1] "The weekday is equal to 2"

結論

在本教程中,我們討論了在 R 中處理日期和時間。我們瞭解了 lubridate 庫中定義的各種函式的工作原理。我希望本教程確實有助於您學習資料科學。

更新於: 2023 年 1 月 17 日

639 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.