Search code examples
rplyr

ifelse in ddply summarize not working when doing max


I'm trying to use ddply to summarize a dataframe in R and its working fine unless I add ifelse to find the maximum datetime (last time) when a field is NOT matching the value, its giving Null in most cases. DOn't know where I'm going wrong.

"last_act_date" = max(ifelse(name == "$view", time, NA)),

Full code is

ahoy_unique_users <- ddply(ahoy_events_acc, .(account_id), summarise, 
                           "start_date" = as.Date(min(time)),
                           "last_date" = as.Date(max(time)),
                           "last_act_date" = max(ifelse(name == "$view", time, NA))
                           )

Solution

  • plyr has been long retired and I would suggest to use dplyr instead. A reproducible example would have been helpful to understand the problem. With dplyr you can try :

    library(dplyr)
    
    ahoy_events_acc %>%
      mutate(time = as.Date(time)) %>%
      group_by(account_id) %>%
      summarise(start_date = min(time), 
                last_date = max(time), 
                last_act_date = max(time[name == "$view"]))
    

    I think last_act_date = max(time[name == "$view"])) should work in your plyr code too.