-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathutils.R
More file actions
177 lines (119 loc) · 4.32 KB
/
Copy pathutils.R
File metadata and controls
177 lines (119 loc) · 4.32 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# UTILITY FUNCTIONS ----
# 1. Mapping Functions -----
grouped_mapper <- function(data, target, .f, ...) {
data <- prep_tbl_time(data, message = FALSE)
target_expr <- dplyr::enquo(target)
group_names <- dplyr::group_vars(data)
ret <- data %>%
dplyr::group_nest() %>%
dplyr::mutate(nested.col = purrr::map(
.x = data,
.f = .f,
target = !! target_expr,
...)
) %>%
dplyr::select(-data) %>%
tidyr::unnest(cols = nested.col) %>%
dplyr::group_by_at(.vars = group_names)
# if (merge) {
# ret <- merge_two_tibbles(tib1 = data, tib2 = ret, .f = .f)
# }
return(ret)
}
# 2. Merging Time-Based Tibbles -----
merge_two_tibbles <- function(tib1, tib2, .f) {
# Merge results
if (identical(nrow(tib1), nrow(tib2))) {
# Arrange dates - Possibility of issue if dates not decending in tib1
tib1 <- arrange_by_date(tib1)
# Drop date column and groups
tib2 <- drop_date_and_group_cols(tib2)
# Replace bad names
tib2 <- replace_bad_names(tib2, .f)
# Replace duplicate names
tib2 <- replace_duplicate_colnames(tib1, tib2)
ret <- dplyr::bind_cols(tib1, tib2)
} else {
stop("Could not join. Incompatible structures.")
}
return(ret)
}
replace_duplicate_colnames <- function(tib1, tib2) {
# Collect column names
name_list_tib1 <- colnames(tib1)
name_list_tib2 <- colnames(tib2)
name_list <- c(name_list_tib1, name_list_tib2)
duplicates_exist <- detect_duplicates(name_list)
# Iteratively add .1, .2, .3 ... onto end of column names
if (duplicates_exist) {
i <- 1
while (duplicates_exist) {
dup_names_stripped <-
strsplit(name_list[duplicated(name_list)],
split = "\\.\\.") %>%
sapply(function(x) x[[1]])
name_list[duplicated(name_list)] <-
paste0(dup_names_stripped, "..", i)
i <- i + 1
duplicates_exist <- detect_duplicates(name_list)
}
name_list_tib2 <- name_list[(ncol(tib1) + 1):length(name_list)]
colnames(tib2) <- name_list_tib2
}
return(tib2)
}
detect_duplicates <- function(name_list) {
name_list %>%
duplicated() %>%
any()
}
# bad / restricted names are names that get selected unintetionally by OHLC functions
replace_bad_names <- function(tib, fun_name) {
bad_names_regex <- "open|high|low|close|volume|adjusted|price"
name_list_tib <- colnames(tib)
name_list_tib_lower <- tolower(name_list_tib)
detect_bad_names <- grepl(pattern = bad_names_regex,
x = name_list_tib_lower)
if (any(detect_bad_names)) {
len <- length(name_list_tib_lower[detect_bad_names])
name_list_tib[detect_bad_names] <- rep(fun_name, length.out = len)
}
colnames(tib) <- name_list_tib
return(tib)
}
arrange_by_date <- function(tib) {
if (dplyr::is.grouped_df(tib)) {
group_names <- dplyr::group_vars(tib)
arrange_date <- function(tib) {
date_col <- timetk::tk_get_timeseries_variables(tib)[[1]]
tib %>%
dplyr::arrange(!! rlang::sym(date_col))
}
tib <- tib %>%
tidyr::nest() %>%
dplyr::mutate(nested.col =
purrr::map(data, arrange_date)
) %>%
dplyr::select(-data) %>%
tidyr::unnest(cols = nested.col) %>%
dplyr::group_by_at(.vars = group_names)
} else {
date_col <- timetk::tk_get_timeseries_variables(tib)[[1]]
tib <- tib %>%
dplyr::arrange(!! rlang::sym(date_col))
}
return(tib)
}
drop_date_and_group_cols <- function(tib) {
date_col <- timetk::tk_get_timeseries_variables(tib)[[1]]
group_cols <- dplyr::groups(tib) %>%
as.character()
cols_to_remove <- c(date_col, group_cols)
tib_names <- colnames(tib)
cols_to_remove_logical <- tib_names %in% cols_to_remove
tib_names_without_date_or_group <- tib_names[!cols_to_remove_logical]
tib <- tib %>%
dplyr::ungroup() %>%
dplyr::select(!!! rlang::syms(tib_names_without_date_or_group))
return(tib)
}