I have a table with a list of transactions across a range of different campaigns (or projects). What I'm trying to do is derive when each campaign was started based on the day of the first transaction in each. The data covers all transactions across all campaigns with only following two columns being relevant: Campaign.ID (numeric), Created.At (yyyy-mm-dd). This is what I have at the moment:
temp$Recency = as.integer(today() - temp$Created.At) # Get number of days ago
df = temp %>% # Multiple transactions per user - we only need the most recent
group_by(Campaign.ID) %>% # Group transactions together by campaign
slice(which.max(temp$Recency)) # Find first transaction - ie biggest Recency value
df = as.data.frame(df)
So I create the temp$Recency
column to change the date column to an integer of days from today. I then group the data based on their campaign ID and try to select only the first transaction for each ie. the one with the largest value in temp$Recency
What's puzzling me is that this code works fine if I use which.min
for finding the last transaction of each campaign and returns a data frame with a single (most recent) instance for each campaign. Yet when I try it with which.max
it returns an empty data frame.
I don't understand the reason for this, surely if which.min
works as expected then which.max
should too. I've tried searching for an answer but haven't come across anyone with the same issue.
The data looks something like this:
Created.At Campaign.ID Recency
2018-06-21 1883 13
2018-06-21 1890 13
2018-06-20 1883 14
There are about ~3000 records across 50-60 different campaigns
which.min returns something like this:
Created.At Campaign.ID Recency
2017-07-02 19 367
2017-05-25 91 405
2017-06-06 344 393
2017-06-30 451 369
2017-06-30 509 369
2017-08-16 551 322
I need the same thing but for which.max