1

I have a table dtTrain which has many columns. I am trying to perform 4 aggregation columns on each, [Min, Max, sd, mean]. To do this I am running the line

subTrain <- dtTrain[,c(min = lapply(.SD, min), 
                       max = lapply(.SD, max), 
                       sd = lapply(.SD, sd), 
                       mean = lapply(.SD, mean)), by=TrialID]

The problem I am having is the aggregation is working but the column headers are repeated (ie column1 is produced four times in the subTrain table). I would prefer [column1.min,...,column1.max...,column1.sd, ...column1.mean,...] or in fact any column label.

Roland
  • 127,288
  • 10
  • 191
  • 288
wbm
  • 155
  • 1
  • 2
  • 13
  • 1
    `names(Result_dataframe) <- c("Min","Max","sd","Mean")` – Sowmya S. Manian Jun 06 '16 at 07:57
  • How about using list() instead of c()? – pe-perry Jun 06 '16 at 08:59
  • @SowmyaS.Manian you can use `setnames` instead of `names<-` – jangorecki Jun 06 '16 at 10:16
  • See also: [Calculate multiple aggregations with lapply(.SD, …)](https://stackoverflow.com/questions/24151602/calculate-multiple-aggregations-with-lapply-sd); [Apply multiple functions to multiple columns in data.table](https://stackoverflow.com/questions/29620783/apply-multiple-functions-to-multiple-columns-in-data-table) – Henrik Nov 28 '18 at 10:30

2 Answers2

1

We can use setnames with rep to change the column names

res <- dtTrain[, c(lapply(.SD, min), 
        lapply(.SD, max),
        lapply(.SD, sd), 
       lapply(.SD, mean)), by=TrialID]
nm1 <- setdiff(names(dtTrain), 'TrialID')
setnames(res, 2:ncol(res), paste(nm1, rep(c('min', 'max', 'sd', 'mean'), 
                         each = length(nm1)), sep="."))  
akrun
  • 874,273
  • 37
  • 540
  • 662
  • res1 <- setnames(res, 1:(length(res)-1), paste(nm1, rep(c('min', 'max', 'sd', 'mean'), each = length(nm1)), sep=".")) works – wbm Jun 06 '16 at 08:00
  • Please, can either akrun or @wbm correct the final answer? Thank you. – Uwe Jun 06 '16 at 08:19
  • 1
    yes, correct. I thought everything in R was zero base but a bit of a newbie. Many thanks @akrun – wbm Jun 06 '16 at 09:49
0

Changing of column names can be done using names() function or colnames() function

 > names(Result_dataframe) <- c("Min","Max","sd","Mean")

 #                         OR

 > colnames(Result_dataframe) <- c("Min","Max","sd","Mean")
Sowmya S. Manian
  • 3,723
  • 3
  • 18
  • 30