Skip to content

Instantly share code, notes, and snippets.

@hannesdatta
Last active December 12, 2019 13:56
Show Gist options
  • Save hannesdatta/875cc81dcacca0362f3d8313483e9051 to your computer and use it in GitHub Desktop.
Save hannesdatta/875cc81dcacca0362f3d8313483e9051 to your computer and use it in GitHub Desktop.
Customized column names when aggregating in data.table
# PROBLEM:
# I would like to give the new column in the new DT
# the name "mean_price"; however, I cannot figure out how to do this.
# It should be possible but I don't know how.
# Here is someone with a related issue: https://stackoverflow.com/questions/12391950/select-assign-to-data-table-when-variable-names-are-stored-in-a-character-vect
# Do you know how to resolve this issue?
# EXAMPLE:
library(data.table)
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_sum <- function(DTx, colname1, p, t) {
DTx[, .(colname1 = mean(get(p))), by = t]
}
result <- compute_price_sum(DT_example, "mean_price", "price", "type")
# SOLUTION
# Write output to a temporary DT, then rename the column.
# Maybe not the most elegant solution, but works.
#
compute_price_sum <- function(DTx, colname1, p, t) {
tmp=DTx[, .(colname1 = mean(get(p))), by = t]
setnames(tmp, 'colname1', colname1)
return(tmp)
}
result <- compute_price_sum(DT_example, "mean_price", "price", "type")
# Other suggestions:
# This function is actually much more general than merely
# to compute prices. I would therefore also give it a more general name.
# Why is it called _sum, by the way? You're taking the mean, right?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment