/
pivot_summary.R
53 lines (42 loc) · 1.2 KB
/
pivot_summary.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
52
53
#' Pivot Summary
#'
#' Wrapper around base r `t()` that returns a tibble.
#' Transposes a data frame, intended for use on the output of a dplyr::summarize operation
#'
#' for an ungrouped summarize, ... argument can be left empty
#' for a grouped summarize, use column names or tidyselect to pivot the group names. Multiple groups
#' will be concatenated before pivoting.
#'
#' @param sumr A tibble
#' @param ... an optional tidyselect specification of grouping columns to pivot
#'
#' @return a tibble
#' @export
pivot_summary <- function(sumr, ...){
sumr %>% dplyr::ungroup(.) -> sumr
column <- rowname <- NULL
if (!missing(..1)) {
sumr %>%
tidyr::unite(col = "column", ..., remove = T) %>% dplyr::relocate(column) -> sumr1
sumr1 %>%
dplyr::select(-1) %>% as.matrix() %>% typeof -> output_mode
}
else{
sumr -> sumr1
}
sumr1 %>%
t() %>%
as.data.frame() %>%
tibble::rownames_to_column() %>%
tibble::as_tibble() %>%
dplyr::rename(column = rowname) -> sumr2
if (!missing(..1)) {
sumr2 %>%
janitor::row_to_names(row_number = 1) %>%
dplyr::mutate(dplyr::across(-1, ~as(., output_mode)))-> sumr3
}
else{
sumr2 -> sumr3
}
sumr3
}