-
Notifications
You must be signed in to change notification settings - Fork 4
/
split_multimodel.R
46 lines (44 loc) · 1.11 KB
/
split_multimodel.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
split_multimodel <- function(.fcst, keep_full = TRUE, ...) {
UseMethod("split_multimodel")
}
split_multimodel.default <- function(df, keep_full = TRUE, df_name) {
sub_models <- unique(
gsub(
"_mbr[[:digit:]]+[[:graph::]]*$",
"",
grep("_mbr[[:digit:]]+", colnames(fcst$awesome_multimodel_eps), value = TRUE)
)
)
if (length(sub_models) > 1) {
get_df <- function(x) {
cols <- grep(
paste(sub_models[sub_models != x], collapse = "|"),
colnames(df),
value = TRUE,
invert = TRUE
)
df[cols]
}
dfs <- lapply(sub_models, get_df)
names(dfs) <- sub_models
if (keep_full) {
dfs[[df_name]] <- df
dfs <- dfs[c(df_name, sub_models)]
}
} else {
dfs <- list(df)
names(dfs) <- df_name
}
structure(dfs, class = "harp_fcst")
}
split_multimodel.harp_fcst <- function(.fcst, keep_full = TRUE) {
.fcst <- mapply(
function(x, y, z) split_multimodel(x, keep_full = z, df_name = y),
.fcst,
names(.fcst),
MoreArgs = list(z = keep_full),
SIMPLIFY = FALSE,
USE.NAMES = FALSE
)
Reduce(c, .fcst)
}