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.

4

2 Answers 2

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="."))  
Sign up to request clarification or add additional context in comments.

3 Comments

res1 <- setnames(res, 1:(length(res)-1), paste(nm1, rep(c('min', 'max', 'sd', 'mean'), each = length(nm1)), sep=".")) works
Please, can either akrun or @wbm correct the final answer? Thank you.
yes, correct. I thought everything in R was zero base but a bit of a newbie. Many thanks @akrun
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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.