简体   繁体   中英

R: for loop with data.table library

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.

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)]

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]])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM