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

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


Hi,

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)
myforetbin[indices[,1],indices[,2]]<-1

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

myforetbin[indices]<-1

However, with:

myforetbin[indices]

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

Can somebody put me on the track?

All the best,

Patrick




More information about the R-help mailing list