有条件地计算R中的行之间的时间差 [英] Conditionally calculate time differences between rows in R

查看:139
本文介绍了有条件地计算R中的行之间的时间差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算具有符合某些条件的列的行和行之间的时差。

I'm trying to calculate the time difference between a row and a row that has a column that meets some criteria.

阅读一些数据:

my_data <- data.frame(criteria = c("some text", "some more text", " ", " ", "more text", " "),
                  timestamp = as.POSIXct(c("2015-07-30 15:53:15", "2015-07-30 15:53:47", "2015-07-30 15:54:48", "2015-07-30 15:55:48", "2015-07-30 15:56:48", "2015-07-30 15:57:49")))

        criteria           timestamp
1      some text 2015-07-30 15:53:15
2 some more text 2015-07-30 15:53:47
3                2015-07-30 15:54:48
4                2015-07-30 15:55:48
5      more text 2015-07-30 15:56:48
6                2015-07-30 15:57:49

我想得到条件列中不为空的每行和最后一行之间的时差(以分钟为单位)。因此,我想要:

I want to get the time difference (in minutes) between every row and the last row that wasn't blank in the criteria column. Therefore, I want:

        criteria           timestamp time_diff
1      some text 2015-07-30 15:53:15         0
2 some more text 2015-07-30 15:53:47         0
3                2015-07-30 15:54:48         1
4                2015-07-30 15:55:48         2
5      more text 2015-07-30 15:56:48         0
6                2015-07-30 15:57:49         1

到目前为止,我已经构建了代码来识别0应该在哪里 - 我只需要代码来填补时间差异。这是我的代码:

So far, I've built the code to recognize where the "0's" should be - I just need the code to fill in the time differences. Here's my code:

my_data$time_diff <- ifelse (my_data$criteria != "", # Here's our statement
  my_data$time_diff <- "0", # Here's what happens if statement is TRUE
  my_data$time_diff <- NEED CODE HERE # if statement FALSE
  )

我有一种感觉,这个工作可能会更好地由不是 ifelse 语句,但我比较新的。

I have a feeling that this job may be better performed by something that isn't an ifelse statement, but i'm relatively new to R.

我在这里找到了q,个人试图在相邻行之间获得时间差异(例如这里此处),但尚未找到有人尝试交易有这种情况。

I've found q's on here where individuals tried to get time differences between neighboring rows (e.g. here and here), but have yet to find someone trying to deal with this kind of situation.

我找到的最接近的问题是,但这个数据与我的不同,个人想要处理它们(至少从我的有利位置)。

The closest question I've found to mine is this one, but that data are different from mine in how the individual wants to process them (at least from my vantage point).

编辑:大写标题

推荐答案

使用alexis_laz的高手表达完成答案:

Completing the answer with alexis_laz's masterful expression:

my_data <- data.frame(criteria = c("some text", "some more text", " ", " ", "more text", " "),
                      timestamp = as.POSIXct(c("2015-07-30 15:53:15", "2015-07-30 15:53:47", "2015-07-30 15:54:48", "2015-07-30 15:55:48", "2015-07-30 15:56:48", "2015-07-30 15:57:49")))

my_data$time_diff <- 
  my_data$timestamp - 
  my_data[cummax((my_data$criteria != " ") * seq_len(nrow(my_data))), 'timestamp']

my_data

        criteria           timestamp time_diff
1      some text 2015-07-30 15:53:15    0 secs
2 some more text 2015-07-30 15:53:47    0 secs
3                2015-07-30 15:54:48   61 secs
4                2015-07-30 15:55:48  121 secs
5      more text 2015-07-30 15:56:48    0 secs
6                2015-07-30 15:57:49   61 secs

这篇关于有条件地计算R中的行之间的时间差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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