I need help in creating a function that classifies a record as TRUE if a record is shows a diff of 7 days per User. Note that the User and DateTime fields are not arranged in order, I just arranged it for easier representation of the dataset.
User DateTime Result
A 2015-05-27 17:13 FALSE
A 2015-06-23 14:17 FALSE
A 2015-06-24 15:44 TRUE
A 2015-06-27 12:16 TRUE
B 2015-03-04 18:07 FALSE
C 2015-07-27 08:26 FALSE
D 2015-03-26 18:13 FALSE
D 2015-05-20 10:35 FALSE
D 2015-05-25 18:07 TRUE
Obviously, my function does not work when I tried this because it just gives me one logical value:
repeatfun <- function(x) {ifelse(sum(diff(x) < 7), TRUE, FALSE)}
Here's the data for easier replication:
User <- c('A', 'A', 'A', 'A', 'A', 'B', 'C', 'D', 'D', 'D', 'D', 'D')
DateTime <- c('2015-05-27', '2015-06-23', '2015-06-24', '2015-06-27', '2015-07-08',
'2015-03-04', '2015-07-27',
'2015-03-26', '2015-05-20', '2015-05-25', '2015-06-17', '2015-08-13')
df <- as.data.frame(cbind(User, DateTime))
df$DateTime <- as.Date(df$DateTime)