6.17 分组聚合
methods("aggregate")
## [1] aggregate.data.frame aggregate.default* aggregate.formula*
## [4] aggregate.ts
## see '?methods' for accessing help and source code
args("aggregate.data.frame")
## function (x, by, FUN, ..., simplify = TRUE, drop = TRUE)
## NULL
args("aggregate.ts")
## function (x, nfrequency = 1, FUN = sum, ndeltat = 1, ts.eps = getOption("ts.eps"),
## ...)
## NULL
# getAnywhere(aggregate.formula)
按 Species 分组,对 Sepal.Length 中大于平均值的数取平均
aggregate(Sepal.Length ~ Species, iris, function(x) mean(x[x > mean(x)]))
## Species Sepal.Length
## 1 setosa 5.313636
## 2 versicolor 6.375000
## 3 virginica 7.159091
library(data.table)
<- data.table(
dt x = rep(1:3, each = 3), y = rep(1:3, 3),
z = rep(c("A", "B", "C"), 3), w = rep(c("a", "b", "a"), each = 3)
)
x_sum = sum(x), y_sum = sum(y)), by = .(z, w)] dt[, .(
## z w x_sum y_sum
## 1: A a 4 2
## 2: B a 4 4
## 3: C a 4 6
## 4: A b 2 1
## 5: B b 2 2
## 6: C b 2 3
x_sum = sum(x), y_sum = sum(y)), by = mget(c("z", "w"))] dt[, .(
## z w x_sum y_sum
## 1: A a 4 2
## 2: B a 4 4
## 3: C a 4 6
## 4: A b 2 1
## 5: B b 2 2
## 6: C b 2 3
shiny 前端传递字符串向量,借助 mget()
函数根据选择的变量分组统计计算,只有一个变量可以使用 get()
传递变量给 data.table
library(shiny)
<- fluidPage(
ui fluidRow(
column(
6,
selectInput("input_vars",
label = "变量", # 给筛选框取名
choices = c(z = "z", w = "w"), # 待选的值
selected = "z", # 指定默认值
multiple = TRUE # 允许多选
),::dataTableOutput("output_table")
DT
)
)
)
library(data.table)
library(magrittr)
<- data.table(
dt x = rep(1:3, each = 3), y = rep(1:3, 3),
z = rep(c("A", "B", "C"), 3), w = rep(c("a", "b", "a"), each = 3)
)
<- function(input, output, session) {
server $output_table <- DT::renderDataTable(
output
{x_sum = sum(x), y_sum = sum(y)), by = mget(input$input_vars)] |>
dt[, .(::datatable()
DT
},server = FALSE
)
}
# 执行
shinyApp(ui = ui, server = server)