When I use diff(), it breaks summarise()

Maybe this is what the question wants. It uses ifelse to compute the difference between values only if the groups have 2 rows, else it returns value unchanged.

library(dplyr)

set.seed(2020)
df <- data.frame(value = runif(10),
                 id = c(
                   sample(1:6, 5),
                   sample(1:6, 5))
)

find <- df %>%
  group_by(id) %>%
  summarise(count = n(), 
            diference = ifelse(count > 1, c(0, diff(value)), value),
            .groups = 'drop') %>%
  filter(count != 2 | diference != 0)

find
## A tibble: 2 x 3
#     id count diference
#  <int> <int>     <dbl>
#1     1     1    0.647 
#2     6     1    0.0674

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top