Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find max of row cumulative sum of adjacent columns by group in R

I have this data frame. What I want to find is the maximum of row cumulative sums for each group given that they are adjacent and have values =1. For example, Group 1 has Year 1,2,3,4 adjacent and values add up =4, and year6 =1 so the max value is 4 for group 1 and so on. Below is an example of data frame and the desired result. Thank you

enter image description here

df <- data.frame(group = c(1:6),
             Year1 = c('1','0','0','0','0','0'),
             Year2 = c('1','1','1','0','1','1'),
             Year3 = c('1','1','0','0','1','0'),
             Year4 = c('1','1','1','0','1','1'),
             Year5 = c('0','0','1','1','1','1'),
             Year6 = c('1','0','0','0','0','1'))

df
      group Year1 Year2 Year3 Year4 Year5 Year6
1     1     1     1     1     1     0     1
2     2     0     1     1     1     0     0
3     3     0     1     0     1     1     0
4     4     0     0     0     0     1     0
5     5     0     1     1     1     1     0
6     6     0     1     0     1     1     1
result <- data.frame(group=c(1:6), value = c(4, 3, 2, 1, 4, 3))

result
  group value
1     1     4
2     2     3
3     3     2
4     4     1
5     5     4
6     6     3

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

We can write a function that will return the maximum of row cumulative sums for each group.

get_max_length <- function(x) {
  rl <- rle(x)
  max(rl$lengths[rl$values == 1])
}

Now we can apply this function for each row.

In base R, using apply

cbind(df[1], value = apply(df[-1], 1, get_max_length))

#  group value
#1     1     4
#2     2     3
#3     3     2
#4     4     1
#5     5     4
#6     6     3

Or with dplyr rowwise :

library(dplyr)

df %>%
  rowwise() %>%
  transmute(group, 
            value = get_max_length(c_across(starts_with("Year")))) %>%
  ungroup()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading