R语言 时间处理 时间分段

Posted 基督徒Isaac

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了R语言 时间处理 时间分段相关的知识,希望对你有一定的参考价值。

library(tidyverse)
library(lubridate) # 转化时区 force_tz()
library(anytime) # 获取日期 anydate()
library(hms) # 获取时间 :as_hms()

a <- data.frame(
  StartTime = c(
    lubridate::ymd_hms("2022-01-31 19:45:00"), # 31日夜班
    lubridate::ymd_hms("2022-02-01 00:00:00"), # 31日夜班
    lubridate::ymd_hms("2022-02-01 07:45:00"), # 01日白班
    lubridate::ymd_hms("2022-02-01 19:45:00"), # 01日夜班
    lubridate::ymd_hms("2022-02-02 00:00:00")  # 01日夜班
  )
); a



a$StartTime[1] # 时区 UTC

# R包实践:lubridate 处理时间数据
# https://site.douban.com/167315/widget/notes/10872119/note/279269803/

lubridate::with_tz(lubridate::ymd_hms("2022-02-01 20:00:00"), tzone='asia/shanghai') # 此时的中国时间
lubridate::force_tz(lubridate::ymd_hms("2022-02-01 20:00:00"), tzone='asia/shanghai') # 强制更改时区

# R- 获取时分秒 - 知乎 hms::as_hms
# https://zhuanlan.zhihu.com/p/365381289

paste(anytime::anydate(a$StartTime[1]) , 
      hms::as_hms(a$StartTime[1])) %>% 
  str # 字符向量,需转化为时间
paste(anytime::anydate(a$StartTime[1]) , 
      hms::as_hms(a$StartTime[1])) %>% 
  lubridate::ymd_hms() %>% 
  lubridate::force_tz(tzone='asia/shanghai')




# 编写函数

baiye <- 
  function(x = lubridate::ymd_hms("2021.12.31 7:40:0")) 
    
    origin <- hms::as_hms("07:45:00")
    TimeStamp = c()
    
    for (i in 1:NROW(x)) 
      
      StartTime <- lubridate::force_tz(x[i], tzone='asia/shanghai')
      date <- anytime::anydate(StartTime)
      
      time <- hms::as_hms(StartTime)
      hour <- (time - origin) / 3600
      
      if (hour >= 0 & hour < 12) 
        stamp <- hms::as_hms("07:45:00")
        TimeStamp[i] <- paste(date, stamp); TimeStamp[i]
        
       else if (hour >= 12) 
        stamp <- hms::as_hms("19:45:00")
        TimeStamp[i] <- paste(date, stamp); TimeStamp[i]
        
       else if (hour < 0) 
        stamp <- hms::as_hms("19:45:00")
        TimeStamp[i] <- paste(date - 1 , stamp); TimeStamp[i]
        
       else  
        stamp <- ""
        TimeStamp[i] <- ""
      
    
    
    # 字符向量,需转化为时间
    y <- lubridate::force_tz(lubridate::ymd_hms(TimeStamp),
                             tzone='asia/shanghai')
    return(y)
  

a <- a %>% mutate(
  TimeStamp = StartTime %>% baiye()
); a

a$TimeStamp # 时间格式,且时区为CST





# debug过程
x <- a$StartTime; x
origin <- hms::as_hms("07:45:00"); origin
TimeStamp = c(); TimeStamp

i = 5; i # 从1到5逐步调试

StartTime <- lubridate::force_tz(x[i], tzone='asia/shanghai'); StartTime
date <- anytime::anydate(StartTime); date # 中国日期

time <- hms::as_hms(StartTime); time
hour <- (time - origin) / 3600; hour

if (hour >= 0 & hour < 12) 
  stamp <- hms::as_hms("07:45:00")
  TimeStamp[i] <- paste(date, stamp); TimeStamp[i]
  
 else if (hour >= 12) 
  stamp <- hms::as_hms("19:45:00")
  TimeStamp[i] <- paste(date, stamp); TimeStamp[i]
  
 else if (hour < 0) 
  stamp <- hms::as_hms("19:45:00")
  TimeStamp[i] <- paste(date - 1 , stamp); TimeStamp[i]
  
 else  
  stamp <- ""
  TimeStamp[i] <- ""
  



print(TimeStamp)

以上是关于R语言 时间处理 时间分段的主要内容,如果未能解决你的问题,请参考以下文章

R语言 | 分段线性回归及对分割点的评估选择及R计算

如何修改R语言 hist()函数横坐标和纵坐标的分段数

R语言时间序列中的时间设置

如何在 R 中使用“组”创建分段图表

R软件可以做分段样条回归吗

难以在 R 中拟合分段线性数据