gpt4 book ai didi

r - 在数据框中添加一个新列,其中包含开始时间戳和结束时间戳之间的天数

转载 作者:行者123 更新时间:2023-12-02 02:10:55 25 4
gpt4 key购买 nike

拥有一个具有如下开始和结束时间戳的数据帧:

df <- data.frame(start = c("2016-09-30 00:00:00", "2016-09-30 00:00:00", "2016-09-30 00:00:00"),  end = c("2017-03-12 00:00:00", "2017-06-30 00:00:00", "2017-12-01 00:00:00"))

如何添加一个新列来显示起点和终点之间的持续时间(以天为单位)?

最佳答案

我们可以使用difftime

library(dplyr)
library(lubridate)
df <- df %>%
mutate(across(everything(), ymd_hms),
diff = as.numeric(difftime(end, start, units = 'days')))
#       start        end diff
#1 2016-09-30 2017-03-12 163
#2 2016-09-30 2017-06-30 273
#3 2016-09-30 2017-12-01 427

关于r - 在数据框中添加一个新列,其中包含开始时间戳和结束时间戳之间的天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67765672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com