r - 如何从分组数据和控制输出格式的最后日期条目中减去第一个日期条目 [英] r - how to subtract first date entry from last date entry in grouped data and control output format

查看:122
本文介绍了r - 如何从分组数据和控制输出格式的最后日期条目中减去第一个日期条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题与另一个线程中提到的问题非常相似,可以在这里。我正在尝试实现类似的事情:在组内(事件)从最后一个日期减去第一个日期。我正在使用该线程的答案中提供的dplyr包和代码。从最后日期起减去第一个日期工作,但不能提供令人满意的结果;所得到的时差显示为数字,并且在不同时间单位(例如,分钟和小时)之间似乎没有区别 - >前2个事件中的减法是正确的,但是在第三个时间单位不应该是分钟。我如何通过dplyr操作输出,以便产生的减法实际上是时差的正确反映?下面你将会找到我的数据示例(仅限1组)和我使用的代码:

This question is very similar to a question asked in another thread which can be found here. I'm trying to achieve something similar: within groups (events) subtract the first date from the last date. I'm using the dplyr package and code provided in the answers of this thread. Subtracting the first date from the last date works, however it does not provide satisfactory results; the resulting time difference is displayed in numbers, and there seems to be no distinction between different time units (e.g., minutes and hours) --> subtractions in first 2 events are correct, however in the 3rd one it is not i.e. should be minutes. How can I manipulate the output by dplyr so that the resulting subtractions are actually a correct reflection of the time difference? Below you will find a sample of my data (1 group only) and the code that I used:

    df<- structure(list(time = structure(c(1428082860, 1428083340, 1428084840, 
1428086820, 1428086940, 1428087120, 1428087240, 1428087360, 1428087480, 
1428087720, 1428088800, 1428089160, 1428089580, 1428089700, 1428090120, 
1428090240, 1428090480, 1428090660, 1428090780, 1428090960, 1428091080, 
1428091200, 1428091500, 1428091620, 1428096060, 1428096420, 1428096540, 
1428096600, 1428097560, 1428097860, 1428100440, 1428100560, 1428100680, 
1428100740, 1428100860, 1428101040, 1428101160, 1428101400, 1428101520, 
1428101760, 1428101940, 1428102240, 1428102840, 1428103080, 1428103620, 
1428103980, 1428104100, 1428104160, 1428104340, 1428104520, 1428104700, 
1428108540, 1428108840, 1428108960, 1428110340, 1428110460, 1428110640
), class = c("POSIXct", "POSIXt"), tzone = ""), event = c(1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3)), .Names = c("time", 
"event"), class = "data.frame", row.names = c(NA, 57L))

  df1 <- df %>%                                                     
  group_by(event) %>%                                           
  summarize(first(time),last(time),difference = last(time)-first(time))


推荐答案

我们可以使用 difftime 并指定单位以获得相同单位之间的所有差异。

We can use difftime and specify the unit to get all the difference in the same unit.

df %>% 
   group_by(event) %>% 
   summarise(First = first(time),
             Last = last(time) , 
             difference= difftime(last(time), first(time), unit='hour'))

这篇关于r - 如何从分组数据和控制输出格式的最后日期条目中减去第一个日期条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆