如何计算以TRUE& amp;为条件的运行总和错误的 [英] How to compute running sum conditional on TRUE & FALSE

查看:41
本文介绍了如何计算以TRUE& amp;为条件的运行总和错误的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个新列,该列是基于TRUE和FALSE列的条件差异.如果滞后1行为FALSE,则我们应该计算与数据行中后来的TRUE行的开头或末尾之间的差,但是如果滞后1行为TRUE,则应重置该差.

I am attempting to create a new column that is a conditional difference based on a column of TRUE and FALSE. If the lag 1 row is FALSE then we should compute a difference from either the beginning or the last TRUE row, whichever is later in the dataframe, however if the lag 1 row is TRUE then the difference should be should be reset.

我想尽可能多地使用dplyr :: mutate函数.我正在尝试将dplyr :: lag与ifelse()一起使用,但是在条件方面我遇到了困难

I would like to use the dplyr::mutate function as much as possible. I'm attempting to use dplyr::lag with an ifelse() but I'm having a hard time with the conditions

 dat <- data.frame(logic_col = c(F, F, T, T, F, F, F, T, F),
                   time_col = c(200, 435, 567, 895, 1012, 1345, 1456, 1700, 1900),
                   expected_col_unseen = c(200, 435, 567, 328, 117, 450, 561, 805, 200))

推荐答案

我们可以使用 tidyr dplyr 做类似的事情:

We can do something like this using tidyr and dplyr:

library(dplyr)
library(tidyr)

dat %>% 
  mutate(tmp = lag(logic_col * time_col),
         tmp = ifelse(tmp==0, NA,tmp)) %>% 
  tidyr::fill(tmp, .direction = c("down")) %>% 
  mutate(out = time_col - ifelse(is.na(tmp), 0,tmp)) %>% 
  select(-tmp)

#>   logic_col time_col expected_col_unseen out
#> 1     FALSE      200                 200 200
#> 2     FALSE      435                 435 435
#> 3      TRUE      567                 567 567
#> 4      TRUE      895                 328 328
#> 5     FALSE     1012                 117 117
#> 6     FALSE     1345                 450 450
#> 7     FALSE     1456                 561 561
#> 8      TRUE     1700                 805 805
#> 9     FALSE     1900                 200 200

这篇关于如何计算以TRUE&amp; amp;为条件的运行总和错误的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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