r语言 - 如何在 data.table 中分配动态列名?



我的目标是编写一个函数,在其中对某些内容进行分组,然后将结果分配给预先指定的列名。下面的函数没有完成此操作,并返回列名为"colname1"的 data.table。有谁知道如何编写列名变为"mean_price"的函数?

DT_example <- data.table(price = c(1,2,3,4,5,6,7,8), type = c("a","a","a","a","b","b","b","b"))
compute_price_mean <- function(DT, colname1, p, t) {
DT[, .(colname1 = mean(get(p))), by = t]
}
result <- compute_price_mean(DT_example, "mean_price", "price", "type")

我们可以使用setNames

library(data.table)
compute_price_sum <- function(DT, colname1, p, t) {
DT_example[, setNames(list(mean(get(p))), colname1), by = t]
}
compute_price_sum(DT_example, "mean_price", "price", "type")
#   type mean_price
#1:    a        2.5
#2:    b        6.5

我们可以使用tidyverse

library(dplyr)
library(data.table)
compute_price_mean <- function(DT, colname1, p, t) {
DT %>%
group_by_at(t) %>% 
summarise(!! colname1 := mean(!! rlang::sym(p)))
}
compute_price_mean(DT_example, "mean_price", "price", "type")
# A tibble: 2 x 2
#  type  mean_price
# <chr>      <dbl>
#1 a            2.5
#2 b            6.5

或者通过指定.SDcolssetnames来使用data.table

compute_price_mean <- function(DT, colname1, p, t) {
setnames(DT[, mean(.SD[[1]]), by = t, .SDcols = p], "V1", colname1)[]
}
#    type mean_price
#1:    a        2.5
#2:    b        6.5      

最新更新