I need to get column names of rows where row has maximum value and This Answer perfectly suits my situation. Only thing is that My data.table has NA in it and I have been trying to get it worked by ignoring NA
Data
set.seed(45)
DT <- data.table(matrix(sample(10, 10^7, TRUE), ncol=10))
DT[, MAX := colnames(.SD)[max.col(.SD, ties.method="first")]]
DT$VX <- NA
Code that works without NA in data
DT[, MAX := colnames(.SD)[max.col(.SD, ties.method="first")]]
below is what I tried with N.A. in data
> DT[, MAX := colnames(.SD)[max.col(replace(.SD, is.na(DT), -Inf), ties.method="first")]]
Warning message:
In max.col(replace(.SD, is.na(DT), -Inf), ties.method = "first") :
NAs introduced by coercion
> DT[, MAX := colnames(.SD)[max.col(.SD,na.rm=TRUE, ties.method="first")]]
Error in max.col(.SD, na.rm = TRUE, ties.method = "first") :
unused argument (na.rm = TRUE)