我有一個正在使用的資料集,我正在嘗試更改時間列的格式。當前格式如下所示,例如:“2022-05-23 23:06:58”,我試圖將其更改為僅顯示小時時間并洗掉日期。
其他資訊:我想在我的資料框中進行此更改,而不僅僅是隨機時間。我想更改超過 100,000 行,所以我需要一個可以這樣做的函式或解決方案。Tidyverse,Lubridate,格式等。謝謝你們。
uj5u.com熱心網友回復:
試試這個第一個問題這里是轉換為一天中的小時的代碼
your_time<-format(as.POSIXct(your_time), format = "%H")
#which gives "23" hours of the day
由于您想應用于大型資料集,我們在下面使用它
large_df%>%
mutate(Hour = format(as.POSIXct(Datetime), format = "%H"))
其中 large_df 是您的大型資料集,價值超過 100,000 條記錄 mutate 將為結果打開另一列,名為 Hour 列,Datetime 是 large_df 資料集中的 DateTime 列
uj5u.com熱心網友回復:
時間作為字串可以嗎?因為那么你可以substr像這樣提取小時和分鐘:
time <- c("2022-05-23 23:02:58", "2022-05-23 13:52:58", "2022-05-23 03:31:58", "2022-05-23 09:09:58")
n <- nchar(time)
hour <- substr(time, n - 7, n - 3)
只需time您的 100.000 行時間列
uj5u.com熱心網友回復:
library(data.table)
hour("2022-05-23 23:06:58") # 23
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/496597.html
