如何根据条件+1 确定列中的值的日期?
我是 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您说“基于条件”,所以我将在条件不成立的地方添加一行。这是一个解决方案,按照评论中的建议使用
as.POSIXlt
:数据
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:Data