0

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)
aprilian
  • 621
  • 3
  • 17
  • 2
    Please show a small reproducible example. Did you meant `is.na(.SD)` – akrun Jul 27 '18 at 17:35
  • 1
    @OmerFarooq The link to `This answer` is not working. Please correct it. – MKR Jul 27 '18 at 17:45
  • 1
    Provide an example where this problem actually appears. `set.seed(45); m = matrix(sample(c(1,NA), 4, replace=TRUE), 2, 2); max.col(replace(m, is.na(m), -Inf), ties.method="first")` works fine, right? – Frank Jul 27 '18 at 18:00
  • You already created a `MAX` column which is `character` string and that could be the reason for `NA` when you do a second time without removing the `MAX` column – akrun Jul 27 '18 at 18:18
  • This works but its outputs column index rather than column index. – aprilian Jul 27 '18 at 18:18
  • Once i remove the `MAX` column (`DT[, MAX := NULL]`) it is working as intended `DT[, names(.SD)[max.col(replace(.SD, is.na(.SD), -Inf), 'first')]]# [1] "V1" "V2" "V8" "V8" "V3" "V8" "V2" "V7" "V10" "V10"` (took a smaller sample)\ – akrun Jul 27 '18 at 18:25
  • Yeah this works fine if MAX is removed. It's strange to me. – aprilian Jul 27 '18 at 18:46

0 Answers0