I am trying to find an efficient way to get a row wise modes on a subset of columns in data.table
#Sample data
a <- data.frame(
id=letters[],
dattyp1 = sample( 1:2, 26, replace=T) ,
dattyp2 = sample( 1:2, 26, replace=T) ,
dattyp3 = sample( 1:2, 26, replace=T) ,
dattyp4 = sample( 1:2, 26, replace=T) ,
dattyp5 = sample( 1:2, 26, replace=T) ,
dattyp6 = sample( 1:2, 26, replace=T)
)
library(modeest)
library(data.table)
I know from: To find "row wise" "Mode" of a given data in R that I can do this:
Mode <- function(x) {
ux <- unique(x)
ux[which.max(tabulate(match(x, ux)))]
}
apply(a[ ,paste0("dattyp",1:6)], 1, Mode)
But this is really slow (over my millions of records). I am thinking there must be a way to do it with .SDcols - but this does column wise modes not row wise.
a<- data.table( a )
a[ , lapply(.SD , mfv ), .SDcols=c(paste0("dattyp",1:6) ) ]