如何根据 lubridate 的时间更改日期时间向量的日期?
How can I alter the date of a datetime vector depending on the time with lubridate?
我正在尝试根据一天中的时间操作日期时间向量中的日期。
向量 newmagic 中的每个项目看起来像这样“2020-03-05 02:03:54 UTC”
对于时间在 19:00 和 23:59 之间的所有项目,我想有一天回去。
我试着写一个 if 语句:
if(hour(newmagic)>=19&hour(newmagic)<=23){
date(newmagic)<-date(newmagic)-1
}
没有输出但是
Warning message: In if (hour(newmagic) >= 19 & hour(newmagic) <= 23) {
: the condition has length > 1 and only the first element will be
used
当我把数据限制在条件下,简单的执行date()-1
newmagic[hour(newmagic)>=19&hour(newmagic)<=23&!is.na(newmagic)] <- date(newmagic[hour(newmagic)>=19&hour(newmagic)<=23&!is.na(newmagic)])-1
输出确实删除了 1 天,但也将时间设置为 0
原文:
"2020-03-07 20:58:00 UTC"
日期()-1之后
"2020-03-06 00:00:00 UTC"
我真的不知道如何继续下去。
我如何调整 if 语句,使其真正按照我的意图进行?
我如何重写第二种方法中的限制,以便时间本身保持不变?
感谢您的帮助
您可以在您的原始数据集中尝试一下。我用过 lubridate
和 tidyverse
包裹。最初我将数据框拆分为日期和时间。然后我将变量转换为日期和时间格式并使用 ifelse
条件。
代码及输出如下:-
library(tidyverse)
library(lubridate)
ab <- data.frame(ymd_hms(c("2000-11-01 2:23:15", "2028-03-25 20:47:51",
"1990-05-14 22:45:30")))
colnames(ab) <- paste(c("Date_time"))
ab <- ab %>% separate(Date_time, into = c("Date", "Time"),
sep = " ", remove = FALSE)
ab$Date <- as.Date(ab$Date)
ab$Time <- hms(ab$Time)
ab$date_condition <- ifelse(hour(ab$Time) %in% c(19,20,21,22,23),
ab$date_condition <- ab$Date -1,
ab$date_condition <- ab$Date)
ab$date_condition <- as.Date(ab$date_condition, format = "%Y-%m-%d",
origin = "1970-01-01")
ab
# Date_time Date Time date_condition
1 2000-11-01 02:23:15 2000-11-01 2H 23M 15S 2000-11-01
2 2028-03-25 20:47:51 2028-03-25 20H 47M 51S 2028-03-24
3 1990-05-14 22:45:30 1990-05-14 22H 45M 30S 1990-05-13
我正在尝试根据一天中的时间操作日期时间向量中的日期。
向量 newmagic 中的每个项目看起来像这样“2020-03-05 02:03:54 UTC” 对于时间在 19:00 和 23:59 之间的所有项目,我想有一天回去。
我试着写一个 if 语句:
if(hour(newmagic)>=19&hour(newmagic)<=23){
date(newmagic)<-date(newmagic)-1
}
没有输出但是
Warning message: In if (hour(newmagic) >= 19 & hour(newmagic) <= 23) { : the condition has length > 1 and only the first element will be used
当我把数据限制在条件下,简单的执行date()-1
newmagic[hour(newmagic)>=19&hour(newmagic)<=23&!is.na(newmagic)] <- date(newmagic[hour(newmagic)>=19&hour(newmagic)<=23&!is.na(newmagic)])-1
输出确实删除了 1 天,但也将时间设置为 0 原文:
"2020-03-07 20:58:00 UTC"
日期()-1之后
"2020-03-06 00:00:00 UTC"
我真的不知道如何继续下去。 我如何调整 if 语句,使其真正按照我的意图进行? 我如何重写第二种方法中的限制,以便时间本身保持不变?
感谢您的帮助
您可以在您的原始数据集中尝试一下。我用过 lubridate
和 tidyverse
包裹。最初我将数据框拆分为日期和时间。然后我将变量转换为日期和时间格式并使用 ifelse
条件。
代码及输出如下:-
library(tidyverse)
library(lubridate)
ab <- data.frame(ymd_hms(c("2000-11-01 2:23:15", "2028-03-25 20:47:51",
"1990-05-14 22:45:30")))
colnames(ab) <- paste(c("Date_time"))
ab <- ab %>% separate(Date_time, into = c("Date", "Time"),
sep = " ", remove = FALSE)
ab$Date <- as.Date(ab$Date)
ab$Time <- hms(ab$Time)
ab$date_condition <- ifelse(hour(ab$Time) %in% c(19,20,21,22,23),
ab$date_condition <- ab$Date -1,
ab$date_condition <- ab$Date)
ab$date_condition <- as.Date(ab$date_condition, format = "%Y-%m-%d",
origin = "1970-01-01")
ab
# Date_time Date Time date_condition
1 2000-11-01 02:23:15 2000-11-01 2H 23M 15S 2000-11-01
2 2028-03-25 20:47:51 2028-03-25 20H 47M 51S 2028-03-24
3 1990-05-14 22:45:30 1990-05-14 22H 45M 30S 1990-05-13