1
votes

I have a data.table with n columns. Every three sequential columns represent triplicate samples. Therefore, I would like to apply a function by row (mean, sd, or a how many of the triplicates are >0) to every 3 columns across the data.table. To be clear, the rows are not groups, the columns are grouped. I am assuming this involves .SDcols, but I can't find a clear example of people doing this with multiple column groups. I could transpose it, but I want to see if there is a simple way to do this without flipping the data.table back and forth. Here is some non-working example code:

DT<-data.table(v0=1:100, v1=rnorm(100), v2=rnorm(100), 
               v3=rnorm(100), v4=rnorm(100), v5=rnorm(100), 
               v6=rnorm(100))
vec<-names(DT[,2:length(DT)])  
vecls<-split(vec, rep(1:(length(vec)/3), each = 3))

DT[,.(Mean = rowMeans(.SD)), by=V0, .SDcols=vecls] 
1

1 Answers

2
votes

An option would be to use split.default

DT[, lapply(split.default(.SD, as.integer(gl(length(vec), 3, length(vec)))), 
                   rowMeans), .SDcols = vec]