'Group by unique ids to calculate number of days in R

i want to find how many days a unique id has worked in this df by abstracting the max date minus min date.

structure(list(id = c("f1", "f2", "f2", "f4", "f5", "f2", "f3", 
"f1", "f1", "f3", "f4", "f2", "f2", "f2", "f2"), 
 date = structure(c(18687, 18687, 18687, 18687, 18687, 
18687, 18687, 18687, 18687, 18687, 18687, 18687, 18687, 18687, 
18687), class = "Date")), row.names = c(2L, 4L, 8L, 15L, 17L, 
18L, 21L, 25L, 36L, 37L, 38L, 40L, 42L, 48L, 52L), class = "data.frame")

I have tried this but I get weird numbers

df_total_days_per_id<-df %>%
  group_by(id) %>%
  mutate(xx1 = max(date)-min(date)) %>% #by user find range
  group_by(id) %>%
  summarise(number_of_days = sum(xx1)) 


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source