File: sort.R

package info (click to toggle)
r-cran-marginaleffects 0.32.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,784 kB
  • sloc: sh: 13; makefile: 8
file content (39 lines) | stat: -rw-r--r-- 1,058 bytes parent folder | download
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
sort_columns <- function(x, newdata = data.frame(), by = NULL) {
    if (!inherits(x, "data.table")) {
        data.table::setDT(x)
    }
    if (isTRUE(checkmate::check_character(by))) {
        bycols <- by
    } else if (isTRUE(checkmate::check_data_frame(by))) {
        bycols <- colnames(by)
    } else {
        bycols <- NULL
    }
    stubcols <- c(
        "rowid",
        "rowidcf",
        "term",
        "group",
        "hypothesis",
        "by",
        grep("^contrast", colnames(x), value = TRUE),
        bycols,
        "estimate",
        "std.error",
        "statistic",
        "p.value",
        "s.value",
        "conf.low",
        "conf.high",
        attr(newdata, "newdata_variables_datagrid"),
        "marginaleffects_wts",
        sort(grep("^predicted", colnames(newdata), value = TRUE))
    )
    cols <- intersect(stubcols, colnames(x))
    cols <- unique(c(cols, colnames(x)))
    x <- x[, ..cols]
    if ("group" %in% names(x) && all(x$group == "main_marginaleffect")) {
        x$group <- NULL
    }
    return(x)
}