Calculate days since last event in R

后端 未结 5 861
别那么骄傲
别那么骄傲 2020-12-17 10:04

My question involves how to calculate the number of days since an event last that occurred in R. Below is a minimal example of the data:

df <- data.frame         


        
5条回答
  •  天命终不由人
    2020-12-17 10:46

    I had a similar issue and was able to solve it combining some of the ideas above. The main difference I had with mine would be customers a - nth would have different events (for me it is purchases). I wanted to know the cumulative totals for all these purchases as well as the date of the last activity. The main way I solved this was to create an index-dataframe to join with the main data frame. Similar to the top rated question above. See repeatable code below.

    library(tidyverse)
    rm(list=ls())
    
    #generate repeatable code sample dataframe
    df <- as.data.frame(sample(rep(sample(seq(as.Date('1999/01/01'), as.Date('2000/01/01'), by="day"), 12), each = 4),36))
    df$subtotal <- sample(1:100, 36)
    df$cust <- sample(rep(c("a", "b", "c", "d", "e", "f"), each=12), 36)
    
    colnames(df) <- c("dates", "subtotal", "cust")
    
    #add a "key" based on date and event
    df$datekey <- paste0(df$dates, df$cust)
    
    #The following 2 lines are specific to my own analysis but added to show depth
    df_total_visits <- df %>% select(dates, cust) %>% distinct() %>% group_by(cust) %>% tally(n= "total_visits") %>% mutate(variable = 1)
    df_order_bydate <-   df %>% select(dates, cust) %>% group_by(dates, cust) %>% tally(n= "day_orders") 
    
    
    df <- left_join(df, df_total_visits)
    df <- left_join(df, df_order_bydate) %>% arrange(dates)
    
    # Now we will add the index, the arrange from the previous line is super important if your data is not already ordered by date
    cummulative_groupping <- df %>% select(datekey, cust, variable, subtotal) %>% group_by(datekey) %>% mutate(spending = sum(subtotal)) %>% distinct(datekey, .keep_all = T) %>% select(-subtotal)
    cummulative_groupping <- cummulative_groupping %>% group_by(cust) %>% mutate(cumulative_visits = cumsum(variable),
                                                                                        cumulative_spend = cumsum(spending))
    
    df <- left_join(df, cummulative_groupping) %>% select(-variable)
    
    #using the cumulative visits as the index, if we add one to this number we can then join it again on our dataframe
    last_date_index <- df %>% select(dates, cust, cumulative_visits)
    last_date_index$cumulative_visits <- last_date_index$cumulative_visits + 1 
    colnames(last_date_index) <- c("last_visit_date", "cust", "cumulative_visits")
    df <- left_join(df, last_date_index, by = c("cust", "cumulative_visits"))
    
    
    #the difference between the date and last visit answers the original posters question.  NAs will return as NA
    df$toa <- df$dates - df$last_visit_date
    

    This answer works in the cases where the same event occurs on the same day (either bad data hygiene OR if multiple vendors/cust go to that event). Thank you for viewing my answer. This is actually my first post on Stack.

提交回复
热议问题