-3

I am using library(data.table) and I would like to do a for loop that calculates the mean of columns 1:4, based on col5 values.

colnames(DT) # "col1" "col2" "col3" "col4" "col5" 
for (i in 1:4){
  o=colnames(DT)[[i]]
  l=DT[,mean(o), by=col5]
  print(l)
}

The problem is that DT does not take colnames as character vectors ("col"). Any advice is appreciated.

Arun
  • 116,683
  • 26
  • 284
  • 387
Kizuna
  • 15
  • 1
  • 6
  • From your code sample , I think it is better for you to take a close in `data.table` package and understand better how it works. This will help you specially if you want to use R in your phd... – agstudy Sep 19 '14 at 23:16
  • possible duplicate of [How to apply same function to every specified column in a data.table](http://stackoverflow.com/questions/16846380/how-to-apply-same-function-to-every-specified-column-in-a-data-table) – bright-star Sep 20 '14 at 00:32

2 Answers2

2

Read the data.table vignettes.

library(data.table)
set.seed(42)
DT <- data.table(matrix(rnorm(100), ncol=4))
setnames(DT, paste0("col", 1:4))
DT[, col5 := rep(1:5, 5)]

DT[, lapply(.SD, mean), by = col5]

DT[, lapply(.SD, mean), by = col5, .SDcols = paste0("col", 2:3)]
Roland
  • 127,288
  • 10
  • 191
  • 288
-2

Try:

DT[,list(mean1=mean(col1), mean2=mean(col2), mean3=mean(col3), mean4=mean(col4)),by=col5]

If columns are too many:

attach(DT)    
for(i in 1:4) print(DT[,mean(get(colnames(DT)[i])),by=DT[,5]])
MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
rnso
  • 23,686
  • 25
  • 112
  • 234