如何计算R中从下午5点到第二天凌晨2点的一天?

时间:2019-12-20 18:19:53

标签: r

我目前正在检查调查参与者的日常进度,以查看人们是否已完成日常调查。人们每天下午5点接受我们的调查,调查在第二天凌晨2点消失。因此,例如,如果某人在12/1/2019接受调查并在12/2/2019的凌晨1点提交调查,则提交日期为12/1/2019。但是,当前的调查程序根据人们的提交时间来计算他们的提交日期。我想使用R编程更改此日期和时间范围。

我的数据中有四列; (ID,日期,日期,时间)。

I have four columns in my data; (ID, Day, Date, Time)

在此先感谢您的帮助!

这里的示例数据为csv

ID,Day,Date,Time
DNP005,1,12/13/2019,01:31:47
DNP005,2,12/14/2019,21:48:33
DNP005,3,12/15/2019,00:10:02
DNP005,4,12/16/2019,00:41:44
DNP005,5,12/17/2019,22:34:10
DNP005,6,12/18/2019,17:54:06
DNP005,7,12/19/2019,00:35:17

dput的输出:

structure(list(ID = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "DNP005", class = "factor"),                                                                                                                  
    Day = 1:7, Date = structure(1:7, .Label = c("12/13/2019",                                                                                                                                                       
    "12/14/2019", "12/15/2019", "12/16/2019", "12/17/2019", "12/18/2019",                                                                                                                                           
    "12/19/2019"), class = "factor"), Time = structure(c(4L,                                                                                                                                                        
    6L, 1L, 3L, 7L, 5L, 2L), .Label = c("00:10:02", "00:35:17",                                                                                                                                                     
    "00:41:44", "01:31:47", "17:54:06", "21:48:33", "22:34:10"                                                                                                                                                      
    ), class = "factor")), class = "data.frame", row.names = c(NA,                                                                                                                                                  
-7L))

2 个答案:

答案 0 :(得分:1)

样本数据:

submissions <- as.POSIXct("2019-12-21 00:01:00", tz="UTC") + 3600*(0:24)
submissions
#  [1] "2019-12-21 00:01:00 UTC" "2019-12-21 01:01:00 UTC" "2019-12-21 02:01:00 UTC"
#  [4] "2019-12-21 03:01:00 UTC" "2019-12-21 04:01:00 UTC" "2019-12-21 05:01:00 UTC"
#  [7] "2019-12-21 06:01:00 UTC" "2019-12-21 07:01:00 UTC" "2019-12-21 08:01:00 UTC"
# [10] "2019-12-21 09:01:00 UTC" "2019-12-21 10:01:00 UTC" "2019-12-21 11:01:00 UTC"
# [13] "2019-12-21 12:01:00 UTC" "2019-12-21 13:01:00 UTC" "2019-12-21 14:01:00 UTC"
# [16] "2019-12-21 15:01:00 UTC" "2019-12-21 16:01:00 UTC" "2019-12-21 17:01:00 UTC"
# [19] "2019-12-21 18:01:00 UTC" "2019-12-21 19:01:00 UTC" "2019-12-21 20:01:00 UTC"
# [22] "2019-12-21 21:01:00 UTC" "2019-12-21 22:01:00 UTC" "2019-12-21 23:01:00 UTC"
# [25] "2019-12-22 00:01:00 UTC"

解决方案:

data.frame(
  submission = submissions,
  submitted = as.Date(submissions),
  survey = as.Date(submissions) - (as.integer(format(submissions, format="%H")) < 2)
)
#             submission  submitted     survey
# 1  2019-12-21 00:01:00 2019-12-21 2019-12-20
# 2  2019-12-21 01:01:00 2019-12-21 2019-12-20
# 3  2019-12-21 02:01:00 2019-12-21 2019-12-21
# 4  2019-12-21 03:01:00 2019-12-21 2019-12-21
# 5  2019-12-21 04:01:00 2019-12-21 2019-12-21
# 6  2019-12-21 05:01:00 2019-12-21 2019-12-21
# 7  2019-12-21 06:01:00 2019-12-21 2019-12-21
# 8  2019-12-21 07:01:00 2019-12-21 2019-12-21
# 9  2019-12-21 08:01:00 2019-12-21 2019-12-21
# 10 2019-12-21 09:01:00 2019-12-21 2019-12-21
# 11 2019-12-21 10:01:00 2019-12-21 2019-12-21
# 12 2019-12-21 11:01:00 2019-12-21 2019-12-21
# 13 2019-12-21 12:01:00 2019-12-21 2019-12-21
# 14 2019-12-21 13:01:00 2019-12-21 2019-12-21
# 15 2019-12-21 14:01:00 2019-12-21 2019-12-21
# 16 2019-12-21 15:01:00 2019-12-21 2019-12-21
# 17 2019-12-21 16:01:00 2019-12-21 2019-12-21
# 18 2019-12-21 17:01:00 2019-12-21 2019-12-21
# 19 2019-12-21 18:01:00 2019-12-21 2019-12-21
# 20 2019-12-21 19:01:00 2019-12-21 2019-12-21
# 21 2019-12-21 20:01:00 2019-12-21 2019-12-21
# 22 2019-12-21 21:01:00 2019-12-21 2019-12-21
# 23 2019-12-21 22:01:00 2019-12-21 2019-12-21
# 24 2019-12-21 23:01:00 2019-12-21 2019-12-21
# 25 2019-12-22 00:01:00 2019-12-22 2019-12-21

仅作为比较错误日期和正确日期的框架显示。

答案 1 :(得分:0)

使用tidyverselubridate

library(tidyverse)
library(lubridate)

# Create test data
df <- data.frame(
    date_completed = c("12/1/2019 17:55:00", "12/2/2019 01:55:00")
    )


 df <- df %>%
    mutate(survey_issued = case_when(
                mdy_hms(date_completed) %>% hour() < 17 ~ (mdy_hms(date_completed) - days(1)) %>% as_date(),
                TRUE ~ mdy_hms(date_completed) %>% as_date()
            )
    )


请注意,这将创建一个新的日期对象,如果您需要保留字符类型,则可以重新转换:


 df <- df %>%
    mutate(survey_issued = case_when(
                mdy_hms(date_completed) %>% hour() < 17 ~ format((mdy_hms(date_completed) - days(1)) %>% as_date(), '%m/%d/%Y'),
                TRUE ~ format(mdy_hms(date_completed) %>% as_date(), '%m/%d/%Y')
            )
    )

编辑:刚从图像的替代文本中注意到,Datetime是单独的列,下面是更新的代码段:

使用日期类型创建新列。

df <- df %>%
    mutate(survey_issued = case_when(
                hms(Time) %>% hour() < 17 ~ (mdy(Date) - days(1)),
                TRUE ~ mdy(Date) %>% as_date()
                )
          )

创建新列并将日期类型转换为字符。

df <- df %>%
    mutate(survey_issued = case_when(
                hms(Time) %>% hour() < 17 ~ (mdy(Date) - days(1)) %>% strftime('%m/%d/%Y') %>% as.character(),
                TRUE ~ Date %>% as.character()
                )
          )