有条件地计算 R 中行之间的时间差
Conditionally calculate time differences between rows in R
我正在尝试计算一行与具有满足某些条件的列的行之间的时间差。
正在读取一些数据:
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
我想获取标准列中非空白的每一行和最后一行之间的时间差(以分钟为单位)。因此,我要:
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”应该在哪里 - 我只需要代码来填充时差。这是我的代码:
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
语句,这项工作可能会更好,但我对 R 比较陌生。
我在这里找到了 q,其中有人试图获得相邻行之间的时间差(例如 here and here),但还没有找到有人试图处理这种情况。
我发现的最接近我的问题是 ,但该数据与我的数据在个人希望如何处理它们方面不同(至少从我的角度来看)。
编辑:标题大写。
用alexis_laz的高超表达完成答案:
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
我正在尝试计算一行与具有满足某些条件的列的行之间的时间差。
正在读取一些数据:
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
我想获取标准列中非空白的每一行和最后一行之间的时间差(以分钟为单位)。因此,我要:
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”应该在哪里 - 我只需要代码来填充时差。这是我的代码:
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
语句,这项工作可能会更好,但我对 R 比较陌生。
我在这里找到了 q,其中有人试图获得相邻行之间的时间差(例如 here and here),但还没有找到有人试图处理这种情况。
我发现的最接近我的问题是
编辑:标题大写。
用alexis_laz的高超表达完成答案:
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