Skip to content

column names of summaryBy #2

@ggrothendieck

Description

@ggrothendieck

Consider this code from here except we have used warpbreaks as the data. The problem is that the first column name is set to the factor name and here we wanted to call it level so that all the data frames generated below have the same column names because we rbindlist all the data frames together. We had to overwrite the names -- see the SetNames line. Perhaps summaryBy could provide some constrol over that through an argument or perhaps if we wrote summaryBy(list(nums, level = x), data, FUN = fun) it could use the name level or whatever name was used there.

library(doBy)
library(data.table)

data <- warpbreaks

facs <- names(Filter(is.factor, data))
nums <- names(Filter(is.numeric, data))

Map(function(x) {
  s <- summaryBy(list(nums, x), data, FUN = list("min", "mean", "median", "max"))
  setNames(s, c("level", names(s)[-1]))
}, facs)
rbindlist(res, idcol = "factor")

##     factor  level breaks.min breaks.mean breaks.median breaks.max
##     <char> <fctr>      <num>       <num>         <num>      <num>
## 1:    wool      A         10    31.03704          26.0         70
## 2:    wool      B         13    25.25926          24.0         44
## 3: tension      L         14    36.38889          29.5         70
## 4: tension      M         12    26.38889          27.0         42
## 5: tension      H         10    21.66667          20.5         43

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions