【问题标题】:How to assign a dynamic column name in a data.table?如何在 data.table 中分配动态列名?
【发布时间】:2020-04-13 14:11:25
【问题描述】:

我的目标是编写一个函数,在其中对某些内容进行分组,然后将结果分配给预先指定的列名。下面的函数没有做到这一点,并返回一个列名为“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")

【问题讨论】:

    标签: r function data.table


    【解决方案1】:

    我们可以使用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
    

    【讨论】:

    • 谢谢!但是你知道更优雅的解决方案吗?
    • 对不起,我不知道你说的“优雅”是什么意思?也许其他人有更好的建议。
    【解决方案2】:

    我们可以使用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      
    

    【讨论】:

      猜你喜欢
      • 2023-02-03
      • 1970-01-01
      • 2012-07-29
      • 2013-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-02
      • 2016-02-02
      相关资源
      最近更新 更多