如何计算条件以R为单位查找两个DateTime之间的差异

问题描述 投票:0回答:1

我模拟数据,并且DateTime是数据库中的日期类型。我想计算两个DateTime之间的分钟数差异,并找到从组中取整数或小数的平均时间。仅凭确认进行计算。

可能类似于此输出数据帧。

这是我的期望图。橙色表示拒绝grom组1

enter image description here

我该怎么办?谢谢您的光临。

sql r datetime difference minute
1个回答
0
投票

您还可以使用dplyrggplot2软件包来完成此操作:另外,如果您的日期是字符串,则可以使用lubridate软件包。

获得差异和平均值:

library(dplyr)
#load if you need to convert strings to dates
library(lubridate)

#df as in example, check 'Data' below
df_new <- df %>%
#counting differences in minutes
  mutate(difference = difftime(ymd_hms(date1), ymd_hms(date2), unit = "mins")) %>%
#grouping dates by 'abc' group
  group_by(group1) %>%
#counting average difference for every 'abc' group in minutes
  mutate(average = mean(difference))

制作情节:

library(ggplot2)

#passing summarized data to ggplot to create a plot and choosing aesthetics / dimensions to be ploted
ggplot(df_new, aes(y = avg_diff, x = group1, fill = group2)) +
#choosing type of plot
  geom_col() +
#flipping x and y axis
  coord_flip() +
#choosing colors
  scale_fill_manual(values = c("green", "orange"))

Result plot

数据:

df <- data.frame(date1 = c("2020-04-05 11:51:51",
                           "2020-04-06 13:55:16",
                           "2020-04-06 14:26:56",
                           "2020-04-06 14:35:05",
                           "2020-04-06 14:36:00",
                           "2020-04-06 14:36:31",
                           "2020-04-06 14:36:31",
                           "2020-04-04 19:00:38",
                           "2020-04-05 21:22:23"),
                 date2 = c("2020-04-05 10:10:23",
                           "2020-04-06 11:41:20",
                           "2020-04-06 14:25:58",
                           "2020-04-06 14:26:03",
                           "2020-04-06 14:32:02",
                           "2020-04-06 14:33:35",
                           "2020-04-06 14:33:35",
                           "2020-04-04 18:30:29",
                           "2020-04-05 21:21:46"),
                 group1 = c("a", "b", "a", "a", "a", "b", "b", "c", "c"),
                 group2 = c("accept", "accept", "accept", "denny", "denny", "accept", "accept", "denny", "denny"),
                 stringsAsFactors = F)
© www.soinside.com 2019 - 2024. All rights reserved.