简体   繁体   中英

Count last rows that meet a criteria in R

I'm trying to make a counter that adds the last rows that meet a criteria. When this criterion is not met, the counter must stop.

Let me explain:

Take this df

df = data.frame(a = c(1,4,3,6,5,3,5,6,2,6,7,1,4,5,7),
                b = c(1,5,5,4,8,5,1,4,8,1,5,4,8,7,6),
                c = c(4,5,5,8,6,1,4,8,5,5,1,5,1,4,7))

I just want to sum this values that meet x > 3 in the last rows:

在此处输入图像描述

So, the result must be:

在此处输入图像描述

i made a code that does this but in a slow way:

df = data.frame(a = c(1,4,3,6,5,3,5,6,2,6,7,1,4,5,7),
                b = c(1,5,5,4,8,5,1,4,8,1,5,4,8,7,6),
                c = c(4,5,5,8,6,1,4,8,5,5,1,5,1,4,7))



df_with_results = data.frame("a" = NA,
                             "b" = NA,
                             "c" = NA)

n_line = 0
count = 0


for(i in 1:ncol(df)){ #loop for each column

  for(k in 0:nrow(df)){ #loop for each row

    
    if(df[(nrow(df)-k), i] > 3) {
      
      count = count + 1
      
      
    } else {
      
      break
      
    }
  
  }
  
  df_with_results[1,i] = count
  
  count = 0 #column change
  
}

any tips?

thanks

We can use rle here

library(dplyr)
sapply(df, \(x) with(rle(x > 3), last(lengths)[last(values)])[1])
a b c 
3 5 2 

One way:

df = data.frame(a = c(1,4,3,6,5,3,5,6,2,6,7,1,4,5,7),
                b = c(1,5,5,4,8,5,1,4,8,1,5,4,8,7,6),
                c = c(4,5,5,8,6,1,4,8,5,5,1,5,1,4,7))

sapply(df, \(x) match(T, rev(x) <= 3, length(x) + 1) - 1)
#> a b c 
#> 3 5 2

Another way:

sapply(df, \(x) sum(cumprod(rev(x)>3)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM