find the first change in value of the group of columns of r

After grouping by ‘id’, select the data columns ‘value1’, ‘value2’, get the rowSums of logical matrix i.e. > 0, convert it to logical vector and get the index of first TRUE value (which) to slice the row

library(dplyr)    
df1 %>% 
    group_by(id) %>%
    slice(which(rowSums(select(cur_data(), value1:value2) > 0) > 0)[1]) %>%
    ungroup

-output

# A tibble: 3 x 4
#     id value1 value2 value3
#  <dbl>  <dbl>  <dbl>  <dbl>
#1     1     10     30     10
#2     4     20      0     10
#3     6      0     20     10

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top