如何操纵日期列的时间部分? [英] How to manipulate the time part of a date column?

查看:117
本文介绍了如何操纵日期列的时间部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何编写此代码(hour来自lubridate软件包)?

How do I write this code (hour is from lubridate package)?

目标:如果PICK_DATE的小时部分晚于16:00,则ADJ_PICK_DATE应该在第二天03:00.如果PICK_DATE的小时部分早于03:00,则ADJ_PICK_DATE为同一天03:00.问题是,当不需要更改时,代码仍会在PICK_DATE添加3个小时,即PICK_DATE的小时部分在03:00和16:00之间.

Objective: if hour part of PICK_DATE is later than 16:00, the ADJ_PICK_DATE should be next day 03:00. If the hour part of PICK_DATE is earlier than 03:00, then ADJ_PICK_DATE is to be same day 03:00. Problem is, when there is no change needed, the code still adds 3 hours to the PICK_DATE i.e. when the hour part of PICK_DATE is within 03:00 and 16:00.

x$PICK_TIME <- cut(hour(x$PICK_DATE), c(-1, 2, 15, 24), c("EARLY", "OKAY", "LATE"))    
x$ADJ_PICK_DATE <- ifelse(x$PICK_TIME=="EARLY",
                          as.POSIXct(paste(format(x$PICK_DATE, "%d-%b-%Y"), "03:00"),
                                 format="%d-%b-%Y %H:%M"), x$PICK_DATE)
x$ADJ_PICK_DATE <- ifelse(x$PICK_TIME=="LATE",
                          as.POSIXct(paste(format(x$PICK_DATE+86400, "%d-%b-%Y"),
                                           "03:00"), format="%d-%b-%Y %H:%M"),
                          x$ADJ_PICK_DATE)
x$ADJ_PICK_DATE <- as.POSIXct(x$ADJ_PICK_DATE, origin = "1970-01-01")

请帮助.

样本数据:

PICK_DATE   SHIP_DATE
01-APR-2017 00:51   02-APR-2017 06:55 AM
01-APR-2017 00:51   02-APR-2017 12:11 PM
01-APR-2017 00:51   02-APR-2017 12:11 PM
01-APR-2017 00:51   02-APR-2017 09:39 AM

推荐答案

这里是一个简单的可复制示例.我必须根据您之前提出的问题来整理一些样本数据.我建议阅读dplyrlubridate,因为它们将帮助您处理日期.

Here is a simple, reproducible example. I had to make up some sample data, based on an earlier question you asked. I suggest reading into dplyr and lubridate as they will help you with your work on manipulating dates.

已更新,可以处理月末日期.

Updated to work with end-of-month dates.

library(lubridate)
library(dplyr)

df <- data.frame(pick_date = c("01-APR-2017 00:51", "02-APR-2017 08:53", "15-APR-2017 16:12", "23-APR-2017 02:04", "30-APR-2017 20:08"), ship_date = c("05-APR-2017 06:55", "09-APR-2017 12:11", "30-APR-2017 13:11", "02-MAY-2017 15:16", "05-MAY-2017 09:57"))

df %>% 
    mutate(pick_date = dmy_hm(pick_date)) %>% 
    mutate(ship_date = dmy_hm(ship_date)) %>% 
    mutate(pick_time = case_when(
        hour(pick_date) <= 3 ~ "early",
        hour(pick_date) >= 16 ~ "late",
        TRUE ~ "okay")
    ) %>% 
    mutate(new_pick_time = case_when(
        pick_time == "early" ~ hms(hours(3)),
        pick_time == "late" ~ hms(hours(3)),
        TRUE ~ hms(paste0(hour(pick_date), "H ", minute(pick_date), "M ", second(pick_date), "S")))
    ) %>% 
    mutate(temp_pick_date = case_when(
        pick_time == "early" ~ pick_date,
        pick_time == "late" ~ pick_date + days(1),
        TRUE ~ pick_date)
    ) %>% 
    mutate(new_pick_date = make_datetime(year(temp_pick_date), month(temp_pick_date), day(temp_pick_date), hour(new_pick_time), minute(new_pick_time), second(new_pick_time))) %>%
    select(-new_pick_time, -temp_pick_date)

这将返回

            pick_date           ship_date pick_time       new_pick_date
1 2017-04-01 00:51:00 2017-04-05 06:55:00     early 2017-04-01 03:00:00
2 2017-04-02 08:53:00 2017-04-09 12:11:00      okay 2017-04-02 08:53:00
3 2017-04-15 16:12:00 2017-04-30 13:11:00      late 2017-04-16 03:00:00
4 2017-04-23 02:04:00 2017-05-02 15:16:00     early 2017-04-23 03:00:00
5 2017-04-30 20:08:00 2017-05-05 09:57:00      late 2017-05-01 03:00:00

这篇关于如何操纵日期列的时间部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆