如何根据条件+1 确定列中的值的日期?

发布于 2025-01-10 15:13:18 字数 507 浏览 6 评论 0原文

我是 R 的初学者。

目前,我正在清理一个围绕航班数据的数据集,并意识到某些行的到达日期/时间早于出发时间的问题。我想为早于 DepDate 的 ArrDate 添加 +1 天。下面是我的 DataFrame 的示例,其中包含 DepDate 和 ArrDate 列。

DepDate               ArrDate
<S3: POSIXct>         <S3: POSIXct>
2006-01-11 22:56:00   2006-01-11 06:55:01
2006-01-11 23:47:00   2006-01-11 06:57:01

我尝试使用此代码,但它似乎不适用于实际的 DataFrame。

df$ArrDate[df$ArrDate < df$DepDate] <- df$ArrDate[df$ArrDate < df$DepDate] + 1

有人可以帮忙吗?谢谢你!

I'm a beginner in R.

Currently, I'm cleaning a dataset that revolves around flight data and realized an issue where arrival date/times for certain rows are earlier than departure times. I want to add +1 days to ArrDate(s) that are earlier than the DepDate(s). Below is the sample of my DataFrame with the DepDate and ArrDate columns.

DepDate               ArrDate
<S3: POSIXct>         <S3: POSIXct>
2006-01-11 22:56:00   2006-01-11 06:55:01
2006-01-11 23:47:00   2006-01-11 06:57:01

I have attempted using this code but it does not seem to apply to the actual DataFrame.

df$ArrDate[df$ArrDate < df$DepDate] <- df$ArrDate[df$ArrDate < df$DepDate] + 1

Can anyone assist with this? Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

念﹏祤嫣 2025-01-17 15:13:18
library(lubridate)
library(data.table)

function_addOneDay = function(x) {
  
  x <- as.POSIXct(x,format = "%Y-%m-%d %H:%M:%OS", tz = "UTC")
  x <- x + days(1)
  
}


df <- fread(
"DepDate|ArrDate
2006-01-11 22:56:00|2006-01-11 06:55:01
2006-01-11 23:47:00|2006-01-11 06:57:01"
)

df <- df[,lapply(.SD, function_addOneDay)]

df
#               DepDate             ArrDate
# 1 2006-01-12 22:56:00 2006-01-12 06:55:01
# 2 2006-01-12 23:47:00 2006-01-12 06:57:01
library(lubridate)
library(data.table)

function_addOneDay = function(x) {
  
  x <- as.POSIXct(x,format = "%Y-%m-%d %H:%M:%OS", tz = "UTC")
  x <- x + days(1)
  
}


df <- fread(
"DepDate|ArrDate
2006-01-11 22:56:00|2006-01-11 06:55:01
2006-01-11 23:47:00|2006-01-11 06:57:01"
)

df <- df[,lapply(.SD, function_addOneDay)]

df
#               DepDate             ArrDate
# 1 2006-01-12 22:56:00 2006-01-12 06:55:01
# 2 2006-01-12 23:47:00 2006-01-12 06:57:01
我家小可爱 2025-01-17 15:13:18

您说“基于条件”,所以我将在条件不成立的地方添加一行。这是一个解决方案,按照评论中的建议使用 as.POSIXlt

library(data.table)
DT
#                DepDate             ArrDate
#                 <POSc>              <POSc>
# 1: 2006-01-11 22:56:00 2006-01-11 06:55:01
# 2: 2006-01-11 23:47:00 2006-01-11 06:57:01
# 3: 2006-01-11 02:56:00 2006-01-11 06:55:01

DT[DepDate > ArrDate,
   ArrDate := as.POSIXct(with(list(r = as.POSIXlt(ArrDate)), { r$mday = r$mday+1; r; })) ]
DT
#                DepDate             ArrDate
#                 <POSc>              <POSc>
# 1: 2006-01-11 22:56:00 2006-01-12 06:55:01
# 2: 2006-01-11 23:47:00 2006-01-12 06:57:01
# 3: 2006-01-11 02:56:00 2006-01-11 06:55:01

数据

DT <- setDT(structure(list(DepDate = structure(c(1137038160, 1137041220, 1136966160), class = c("POSIXct", "POSIXt"), tzone = ""), ArrDate = structure(c(1136980501, 1136980621, 1136980501), class = c("POSIXct", "POSIXt"), tzone = "")), row.names = c(NA, -3L), class = c("data.table", "data.frame"))) 

You said "based on conditions", so I'll add a row where the conditions do not hold. Here's a solution, using as.POSIXlt as suggested in the comments:

library(data.table)
DT
#                DepDate             ArrDate
#                 <POSc>              <POSc>
# 1: 2006-01-11 22:56:00 2006-01-11 06:55:01
# 2: 2006-01-11 23:47:00 2006-01-11 06:57:01
# 3: 2006-01-11 02:56:00 2006-01-11 06:55:01

DT[DepDate > ArrDate,
   ArrDate := as.POSIXct(with(list(r = as.POSIXlt(ArrDate)), { r$mday = r$mday+1; r; })) ]
DT
#                DepDate             ArrDate
#                 <POSc>              <POSc>
# 1: 2006-01-11 22:56:00 2006-01-12 06:55:01
# 2: 2006-01-11 23:47:00 2006-01-12 06:57:01
# 3: 2006-01-11 02:56:00 2006-01-11 06:55:01

Data

DT <- setDT(structure(list(DepDate = structure(c(1137038160, 1137041220, 1136966160), class = c("POSIXct", "POSIXt"), tzone = ""), ArrDate = structure(c(1136980501, 1136980621, 1136980501), class = c("POSIXct", "POSIXt"), tzone = "")), row.names = c(NA, -3L), class = c("data.table", "data.frame"))) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文