Home > Mobile >  Count last rows that meet a criteria in R
Count last rows that meet a criteria in R

Time:12-06

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:

enter image description here

So, the result must be:

enter image description here

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

CodePudding user response:

We can use rle here

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

CodePudding user response:

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)))

  •  Tags:  
  • r
  • Related