vendredi 27 mars 2015

turning the non-zeros of a vector into 1's

suppose I create the following vector:



x = rbind(1, 3, 5, 0, 7, 8, 0, 4, 5, 0, 8)
x


I want to turn the non-zeros into ones:



for ( i in 1:length(x) ){
if (x[i]!=0) x[i] = 1}
x


This works fine, but I was wondering if anyone out there can think of a mathematical way of doing this operation instead of using the if statement.


Aucun commentaire:

Enregistrer un commentaire