[R] which() and value replacement in a matrix

Patrick Giraudoux patrick.giraudoux at univ-fcomte.fr
Sat Oct 9 12:28:49 CEST 2004


I cannot go through the archives with which() as key-word... so common. Though I am sure to have seen something about this subject
in the past could somebody put me on the track. I have a matrix (actually a data.frame) in which I would replace the non-null values
by 1.

I tried the following:

indices<-which(myforetbin > 0,arr.ind=T)

and get the message:

> myforetbin[indices[,1],indices[,2]]<-1
Error in "[<-.data.frame"(`*tmp*`, indices[, 1], indices[, 2], value = 1) :
        duplicate subscripts for columns

I get the same with


However, with:


I well get a vector with the corresponding non-null values.

Can somebody put me on the track?

All the best,


More information about the R-help mailing list