简体   繁体   English

如何计算条件在 R 中以分钟为单位找到两个 DateTime 之间的差异

[英]How to calculate condition find the difference between two DateTime in minute in R

I simulate data and DateTime is the date type from the database.我模拟数据,DateTime 是数据库中的日期类型。 I want to calculate the difference of minutes between two DateTime and find average times to integer or decimal from the group.我想计算两个 DateTime 之间的分钟差,从组中找到 integer 或小数的平均时间。
It calculates with an only confirmation.它仅通过确认进行计算。

maybe similar to this output data frame.可能类似于此 output 数据帧。

and this is my expectation graph.这是我的期望图。 Orange color is deny grom group1橙色是拒绝 grom group1

在此处输入图像描述

How can I do it?我该怎么做? Thank you for coming.谢谢你的到来。

You can also accomplish this with use of dplyr and ggplot2 packages: Additionaly, you can use lubridate package, if your dates are strings.您还可以使用dplyrggplot2包来完成此操作:此外,如果您的日期是字符串,您可以使用lubridate package。

To get differences and averages:要获得差异和平均值:

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))

To make plot:制作 plot:

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"))

结果图

Data:数据:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何找到两个日期之间的差异? 计算找到两个计数之间的差异? - How to find the difference between two dates ? Calculate the find the difference between two Counts? 如何在两列数据类型都是日期的 Oracle 中的两列之间找到分钟到两位小数的差异? - How to find difference in minute up to two decimal between two column in Oracle where both column datatype is date? SQL语句来计算同一表的两行之间的DateTime差异 - SQL Statement to Calculate DateTime Difference between Two Rows of the same Table 计算每天两个日期时间字段之间的平均时差 - Calculate average time difference between two datetime fields per day HiveQL - 根据条件计算两行之间的时间差 - HiveQL - Calculate Time Difference Between Two Rows Based On A Condition 如何计算SQL中两行之间的差异? - How to calculate difference between two rows in SQL? 如何计算两个日期之间的差异 - How to calculate difference between two dates 找出两个日期时间之间的分钟差 - Find the minute difference between 2 date time 如何根据开始和结束日期时间计算值之间的差异 - How to calculate difference between a value, based on start and end datetime 如何在某些条件下找到两个不同列中日期之间的最小差异 - How to find the minimum difference between dates in two different columns with some condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM