Home > Back-end >  Find column name with value bigger than zero in each row
Find column name with value bigger than zero in each row

Time:04-14

I have data as follows:

dat <- structure(list(rn = c("A", "B", 
"C", "D", "E", 
"F", "G", "H", 
"I", "J", "K", 
"L", "M", "N"
), `0` = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L), `1` = c(0L, 0L, 0L, 0L, 0L, 0L, 569L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L), `2` = c(0L, 0L, 0L, 238L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L), `3` = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1146L, 0L, 
0L, 0L, 0L, 0L), `4` = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 337L, 0L, 0L), `5` = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 631L), `6` = c(0L, 0L, 0L, 0L, 156L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), `7` = c(0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 298L, 0L, 0L, 0L), `8` = c(0L, 0L, 0L, 0L, 
0L, 456L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), `9` = c(0L, 0L, 0L, 
0L, 0L, 0L, 0L, 927L, 0L, 0L, 0L, 0L, 0L, 0L), `10` = c(436L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), `11` = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 657L, 0L, 0L, 0L, 0L), `12` = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1771L, 0L), `13` = c(0L, 
0L, 283L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), `14` = c(0L, 
297L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), class = c("data.table", 
"data.frame"), row.names = c(NA, -14L))

    rn 0   1   2    3   4   5   6   7   8   9  10  11   12  13  14
 1:  A 0   0   0    0   0   0   0   0   0   0 436   0    0   0   0
 2:  B 0   0   0    0   0   0   0   0   0   0   0   0    0   0 297
 3:  C 0   0   0    0   0   0   0   0   0   0   0   0    0 283   0
 4:  D 0   0 238    0   0   0   0   0   0   0   0   0    0   0   0
 5:  E 0   0   0    0   0   0 156   0   0   0   0   0    0   0   0
 6:  F 0   0   0    0   0   0   0   0 456   0   0   0    0   0   0
 7:  G 0 569   0    0   0   0   0   0   0   0   0   0    0   0   0
 8:  H 0   0   0    0   0   0   0   0   0 927   0   0    0   0   0
 9:  I 0   0   0 1146   0   0   0   0   0   0   0   0    0   0   0
10:  J 0   0   0    0   0   0   0   0   0   0   0 657    0   0   0
11:  K 0   0   0    0   0   0   0 298   0   0   0   0    0   0   0
12:  L 0   0   0    0 337   0   0   0   0   0   0   0    0   0   0
13:  M 0   0   0    0   0   0   0   0   0   0   0   0 1771   0   0
14:  N 0   0   0    0   0 631   0   0   0   0   0   0    0   0   0

I want to create a column with the column name of the column in which there is a value greater than zero.

Desired output:

dat <- structure(list(rn = c("A", "B", 
"C", "D", "E", 
"F", "G", "H", 
"I", "J", "K", 
"L", "M", "N"
), NR = c(10, 14, 13, 2, 6, 8, 1, 9, 3, 11, 7, 4, 12, 
5)), class = c("data.table", 
"data.frame"), row.names = c(NA, -14L))

    rn NR
 1:  A 10
 2:  B 14
 3:  C 13
 4:  D  2
 5:  E  6
 6:  F  8
 7:  G  1
 8:  H  9
 9:  I  3
10:  J 11
11:  K  7
12:  L  4
13:  M 12
14:  N  5

CodePudding user response:

Easier is with max.col from base R

library(data.table)
dat[, .(rn, NR = max.col(.SD[,-1, with = FALSE] > 0, "first")-1)]

-output

     rn    NR
    <char> <num>
 1:      A    10
 2:      B    14
 3:      C    13
 4:      D     2
 5:      E     6
 6:      F     8
 7:      G     1
 8:      H     9
 9:      I     3
10:      J    11
11:      K     7
12:      L     4
13:      M    12
14:      N     5

Or another option is apply from base R

apply(dat[, -1], 1, \(x) which(x > 0)[1])-1
 [1] 10 14 13  2  6  8  1  9  3 11  7  4 12  5
  •  Tags:  
  • r
  • Related