I have a dataframe with users and their visit dates. I'm trying to find the average time difference between visits per group. The output would be in days or fraction of a day
require(lubridate)
so <- data.frame(visit_dates = c("12/4/2016","12/6/2016","12/7/2016","12/3/2016","12/7/2016","12/10/2016"), person = c("1","1","1","2","2","2"))
so$visit_dates <- mdy(format(as.POSIXct(strptime(so$visit_dates,"%m/%d/%Y",tz = "")),format = "%m/%d/%Y"))
The output would look something like:
person avgTimeBetweenVisit
1 2.5
2 3.5
Try data.table:
require(lubridate)
require(data.table)
so <- data.frame(visit_dates = c("12/4/2016","12/6/2016","12/7/2016","12/3/2016","12/7/2016","12/10/2016"), person = c("1","1","1","2","2","2"))
so$visit_dates <- mdy(format(as.POSIXct(strptime(so$visit_dates,"%m/%d/%Y",tz = "")),format = "%m/%d/%Y"))
so <- data.table(so, key = c("person", "visit_dates"))
res <- so[, .(avgTimeBetweenVisit = mean(diff(visit_dates))), by = person]
print(res)
# person avgTimeBetweenVisit
# 1: 1 1.5 days
# 2: 2 3.5 days
What about that:
so %>%
group_by(person)%>%
mutate(Difference = visit_dates - lag(visit_dates)) %>%
summarize(mean_time = mean(Difference, na.rm=TRUE))
That link helped me with the 'diff' problem. diff operation within a group, after a dplyr::group_by()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With