简体   繁体   English

如何在R中满足特定条件时获得连续出现的平均值

[英]How to get the average of consecutive occurrences when meet certain condition in R

I have a data that has payment schedule for customers that is ordered by transaction date.I want to calculate the average number of consecutive failed payments and average number of consecutive success payments.The table looks like below:我有一个data ,其中包含按交易日期订购的客户付款时间表。我想计算连续失败付款的平均次数和连续成功付款的平均次数。表格如下所示:

customer_id |transaction_id.|failed_or_success  | transaction_date 
  1         |1              |success            |2021-01-01
  1         |2              |success            |2021-01-15
  1         |3              |failed             |2021-01-30
  1         |4              |success            |2021-02-15

For example, the average number of consecutive success payment would be (2+1)/2=1.5 , the first 2 comes from transaction_id 1 & 2.the second 1 comes from transaction_id 4. And the average number of consecutive failed payment would just be 1 in this example.例如,平均连续支付成功次数为(2+1)/2=1.5 ,前2来自 transaction_id 1 & 2,第二个1来自 transaction_id 4。而连续支付失败的平均次数为在本例中为 1。 Eventually the table would look like this:最终表格将如下所示:

cus_id |tran_id.|f_or_s |tran_date  |avg_consec_fail|avg_consec_success
  1    |1       |success|2021-01-01 |1              |1.5
  1    |2       |success|2021-01-15 |1              |1.5
  1    |3       |failed |2021-01-30 |1              |1.5
  1    |4       |success|2021-02-15 |1              |1.5

How do I make this happen with R/dplyr ?我如何使用R/dplyr实现这一点?

You may try using rle您可以尝试使用rle

Data数据

df <- read.table(text = "customer_id transaction_id. failed_or_success   transaction_date 
  1         1              success            2021-01-01
  1         2              success            2021-01-15
  1         3              failed             2021-01-30
  1         4              success            2021-02-15", header = TRUE)

Code代码

df %>%
  mutate(avg_consec_success = mean(rle(failed_or_success)$length[rle(failed_or_success)$values != "failed"]))

  customer_id transaction_id. failed_or_success transaction_date avg_consec_success
1           1               1           success       2021-01-01                1.5
2           1               2           success       2021-01-15                1.5
3           1               3            failed       2021-01-30                1.5
4           1               4           success       2021-02-15                1.5

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

相关问题 当(A)是R中data.frame的特定条件时,如何使函数获得平均值(B) - How to make a function to get average (B) when (A) is a certain condition on data.frame in R R:如何用长度条件计算纵向数据库中连续出现的次数? - R: How to count the number of consecutive occurrences in a longitudinal database with a length condition? 如何计算R中某些连续数据值的平均值 - How to Calculate average of certain consecutive data values in R 如果满足条件,如何对连续行进行子集化 - How to subset consecutive rows if they meet a condition R中满足一定条件则省略分组 - Omit groups if they meet a certain condition in R R中满足一定条件的列组合 - Combinations of columns that meet a certain condition in R 如何获取列的最大值,当该列中的某个值在R中满足时重置最大值 - How to get the max value of a column, reset the max value when certain value in this column is meet in R 如何使用 R 提取一组 10 个连续数据,其中 5 个满足特定条件? - How can I use R to extract a set of 10 consecutive data where 5 out of them meet certain criteria? 如何获取满足特定条件的 R 数据帧中的第一行? - How to get the first rows in an R dataframe that meet a specific condition? 如何在 R 中计算特定条件下的平均值? - How do i calculate the average with certain condition in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM