Get the minimum and maximum date for each user

Hi Guys,

I'm working with a table that has in one column the user ids and in other colum the dates on which each user has uploaded a record

imagen

What I want is to have a new table where I have for each user, their oldest registered date and their most current date. Try the "mutate" function as follows:

df %>% mutate( fecha_inicial = min(observed_on) , fecha_final = max(observed_on)

But the result is that it takes the minimum and maximum date it finds on the entire data set

I don't know how to apply it to each different user

I know it must be something very easy but I would appreciate your help

Thank you :slight_smile:

Add group_by() and you should get your intended result.

df %>% 
  group_by(user_id) %>% 
  mutate(fecha_inicial = min(observed_on), 
         fecha_final = max(observed_on)
         ) %>% 
  ungroup() %>%
  distinct(user_id, fecha_inicial, fecha_final)

Alternatively use summarize instead of mutate and then distinct:

df %>% 
  group_by(user_id) %>% 
  summarize(
        fecha_inicial = min(observed_on), 
         fecha_final = max(observed_on)
         )

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.