用于根据销售数据创建摘要 table 的函数
function for creating a summary table from sales data
我正在寻找创建摘要 table 的函数,我可以在其中将 table 作为输入参数传递并创建如下摘要,我正在尝试使用 dcast 创建但没有能够使变量动态化。
示例数据框
target <- c("DT 0", "DT 1", "DT 2", "DT 3", "DT 4", "DT 5", "DT 6", "DT 7", "DT 8")
basic <- c(19,50,79,80,72,30,13,45,52)
beginner <- c(22,82,50,15,51,54,40,17,31)
medium <- c(18,66,29,32,40,34,57,60,84)
projected <- c(38,63,17,64,81,41,14,63,79)
Planned <- c(53,12,73,34,16,77,12,60,88)
df <- data.frame(target,basic,beginner,medium,projected,Planned)
df <- as.data.frame(t(df))
所需的输出应如下所示
您可以使用:
library(dplyr)
library(tidyr)
df %>%
select(target, basic, Planned) %>%
pivot_wider(names_from = target, values_from = c(basic, Planned),
names_glue = '{target}_{.value}') %>%
select(order(stringr::str_extract(names(.), 'DT \d+')))
如果您使用的是数据的转置版本,请使用以下代码。
df %>%
rownames_to_column('val') %>%
filter(val %in% c('target', 'basic', 'Planned')) %>%
t %>%
as.data.frame() %>%
setNames(.[1, ]) %>%
slice(-1) %>%
pivot_wider(names_from = target, values_from = c(basic, Planned),
names_glue = '{target}_{.value}') %>%
select(order(stringr::str_extract(names(.), 'DT \d+')))
R 数据帧不能有多个标题,如果您希望输出看起来与显示的完全一样,您可以查看 knitr::kable
或 gt::gt
等函数和其他函数来显示漂亮的表格。
这是我能想出的最好的办法,但是到目前为止我无法从第二个 header 中删除 DT_
部分,因为名称应该是唯一的并且它会抛出错误。我会尽快弄清楚的。
library(dplyr)
library(tidyr)
library(kableExtra)
df %>%
pivot_longer(- target, names_to = "summary", values_to = "Banking") %>%
filter(summary %in% c("basic", "Planned"),
target %in% c("DT 0", "DT 5", "DT 8")) %>%
pivot_wider(names_from = c("target", "summary"), values_from = Banking) %>%
mutate(`Sales Review` = "Banking") %>%
relocate(`Sales Review`) %>%
kbl() %>%
kable_classic() %>%
add_header_above(c(" "= 1, "DT 0" = 2, "DT 5" = 2, "DT 8" = 2))
我正在寻找创建摘要 table 的函数,我可以在其中将 table 作为输入参数传递并创建如下摘要,我正在尝试使用 dcast 创建但没有能够使变量动态化。
示例数据框
target <- c("DT 0", "DT 1", "DT 2", "DT 3", "DT 4", "DT 5", "DT 6", "DT 7", "DT 8")
basic <- c(19,50,79,80,72,30,13,45,52)
beginner <- c(22,82,50,15,51,54,40,17,31)
medium <- c(18,66,29,32,40,34,57,60,84)
projected <- c(38,63,17,64,81,41,14,63,79)
Planned <- c(53,12,73,34,16,77,12,60,88)
df <- data.frame(target,basic,beginner,medium,projected,Planned)
df <- as.data.frame(t(df))
所需的输出应如下所示
您可以使用:
library(dplyr)
library(tidyr)
df %>%
select(target, basic, Planned) %>%
pivot_wider(names_from = target, values_from = c(basic, Planned),
names_glue = '{target}_{.value}') %>%
select(order(stringr::str_extract(names(.), 'DT \d+')))
如果您使用的是数据的转置版本,请使用以下代码。
df %>%
rownames_to_column('val') %>%
filter(val %in% c('target', 'basic', 'Planned')) %>%
t %>%
as.data.frame() %>%
setNames(.[1, ]) %>%
slice(-1) %>%
pivot_wider(names_from = target, values_from = c(basic, Planned),
names_glue = '{target}_{.value}') %>%
select(order(stringr::str_extract(names(.), 'DT \d+')))
R 数据帧不能有多个标题,如果您希望输出看起来与显示的完全一样,您可以查看 knitr::kable
或 gt::gt
等函数和其他函数来显示漂亮的表格。
这是我能想出的最好的办法,但是到目前为止我无法从第二个 header 中删除 DT_
部分,因为名称应该是唯一的并且它会抛出错误。我会尽快弄清楚的。
library(dplyr)
library(tidyr)
library(kableExtra)
df %>%
pivot_longer(- target, names_to = "summary", values_to = "Banking") %>%
filter(summary %in% c("basic", "Planned"),
target %in% c("DT 0", "DT 5", "DT 8")) %>%
pivot_wider(names_from = c("target", "summary"), values_from = Banking) %>%
mutate(`Sales Review` = "Banking") %>%
relocate(`Sales Review`) %>%
kbl() %>%
kable_classic() %>%
add_header_above(c(" "= 1, "DT 0" = 2, "DT 5" = 2, "DT 8" = 2))