試圖產生一個輸出來測驗 date_1 是在 AM 還是 PM。這是我擁有的代碼,但它不起作用。如果日期是上午,我想要“它在上午”,如果不是,我想要輸出,“它是下午”
date_1 <- as.Date("2021/05/25 14:34:25")
ifelse(date_1 == AM, "It is in the AM","It is the PM" )
uj5u.com熱心網友回復:
您的日期是一個日期時間物件。
與lubridate...
library(lubridate, warn = FALSE)
dt_1 <- ymd_hms(c("2021/05/25 14:34:25", "2021/05/25 00:34:25"))
ifelse(am(dt_1), "It is in the AM","It is the PM" )
#> [1] "It is the PM" "It is in the AM"
基礎 R 選項...
dt_1 <- as.POSIXlt(c("2021/05/25 14:34:25", "2021/05/25 00:34:25"))
ifelse(dt_1$hour >= 0 & dt_1$hour < 12, "It is in the AM","It is the PM" )
#> [1] "It is the PM" "It is in the AM"
使用reprex v2.0.2創建于 2022-10-15
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/515285.html
標籤:r
下一篇:ggplot中的氣泡圖基礎知識
