Pavan Ebbadi Pavan Ebbadi - 8 days ago 5
R Question

R data.table, Replacing each cell value if greater than 0 with 1

I have R data.table
There are 25 columns, 1st column is ID, 24 columns are integer variables. There are close to 1Million rows. How do i convert all these non-zero values to 1

Custid A B C
123 0 8 0
124 0 0 6


Should become

Custid A B C
123 0 1 0
124 0 0 1

Answer

An efficient option is the set function which replace in place. Loop through the columns using a for loop and set the 'value' to 1 where the element is not equal to 0 by specifying the 'i' and 'j' index.

for(j in 2:ncol(dt)){
   set(dt, i= which(dt[[j]]!=0), j=j, value =1)
}
dt
#   Custid A B C
#1:    123 0 1 0
#2:    124 0 0 1
Comments