-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_breaks.R
51 lines (41 loc) · 1.58 KB
/
get_breaks.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
get_breaks <- function(df,
break_column,
x_col,
break_date = NULL,
already_grouped = TRUE,
subgroup_on = "month") {
# catch always NULL at start
if (is.null(break_column)) {
return (NULL)
}
# break on user input date
if (break_column == "Choose date on calendar") {
if (!is.null(break_date)) {
if (already_grouped) {
dataDates <- as.Date(arrange_at(df, x_col)[[x_col]])
breaks <- which.min(abs(dataDates - as.Date(break_date)))
} else if (is.null(subgroup_on)) {
return(NULL)
} else {
only_dates <- as_datetime(arrange_at(df, x_col)[[x_col]])
changed_dates <- cut.POSIXt(only_dates, subgroup_on) %>% unique()
breaks <- which.min(abs(as.Date(changed_dates) - as.Date(break_date)))
}
} else {
return(NULL) # user hasn't picked date yet
}
# break on column
} else {
cutoffDates <- (df %>% group_by_at(break_column) %>% summarise_at(x_col, max) %>% arrange_at(x_col))[[x_col]]
if (already_grouped) {
dataDates <- arrange_at(df, x_col)[[x_col]]
} else if (is.null(subgroup_on)) {
return(NULL)
} else {
cutoffDates <- cut.POSIXt(as_datetime(cutoffDates), subgroup_on)
dataDates <- (df %>% mutate_at(x_col, cut.POSIXt, subgroup_on) %>% group_by_at(x_col) %>% summarise_at(break_column, length) %>% arrange_at(x_col))[[x_col]]
}
breaks <- which(dataDates %in% cutoffDates)
}
return(breaks)
}