Thank you so much for your help in advance!
I am trying to modify an existing matrix such that, when a new line is added to the matrix, it removes values from the preexisting matrix.
For example, I have the matrix:
[,1] [,2] [,3] [,4]
1 1 0 0
0 1 0 0
1 0 1 0
0 0 1 1
I want to add another vector, I.vec, which has two values (I.vec=c(0,1,1,0)).
This is easy enough to do. I just rbind it to the matrix.
Now, for every column where I.vec is equal to 1, I want to randomly select a value from the other rows and make it zero.
Ideally, this would end up with a matrix like:
[,1] [,2] [,3] [,4]
1 0 0 0
0 1 0 0
1 0 0 0
0 0 1 1
0 1 1 0
But each time I run the iteration, I want it to randomly sample again.
So this is what I have tried:
mat1<-matrix(c(1,1,0,0,0,1,0,0,1,0,1,0,0,0,1,1),byrow=T, nrow=4)
I.vec<-c(0,1,1,0)
mat.I<-rbind(mat1,I.vec)
mat.I.r<-mat.I
d1<-mat.I[,which(mat.I[5,]==1)]
mat.I.r[sample(which(d1[1:4]==1),1),which(mat.I[5,]==1)]<-0
But this only deletes one of the two values I would like to delete. I have also tried variations on subsetting the matrix, but I have not been successful.
Thank you again!
There is a little bit of ambiguity in the description from the OP, so two solutions are suggested:
Assuming that only existing
1s in relevant columns can be set to0I’ll just alter the original function (see below). The change is to the line defining
rows. I now have (there was a bug in the original – the version below is revised to handle deal with the bug):Basically, what this does is, for each column we need to swap a
1to a0, we work out which rows of the column contain1s and sample one of these.Edit: We have to handle the case where there is only a single
1in a column. If we just sample from a length 1 vector, R’ssample()will treat it as if we wanted to sample from the setseq_len(n)not from the length 1 setn. We handle this now with anif, elsestatement.We have to do this individually for each column so we get the correct rows. I suppose we could do some nice manipulation to avoid repeated calls to
which()andsample(), but how escapes me at the moment, because we do have to handle the case where there is only one1in the column. Here’s the finished function (updated to handle the length 1 sample bug in the original):and here it is in action:
and it works when there is only one
1in a column we want to do a swap in:Original Answer: Assuming any value in a relevant column can be set to zero
Here is a vectorised answer, where we treat the matrix as a vector when doing the replacement. Using the example data:
This gives us:
If I were doing this more than once or twice, I’d wrap this in a function:
Which gives:
If you wanted to do this for multiple
ivecs, growingmat1each time, then you probably don’t want to do that in a loop as growing objects is slow (it involves copies etc). But you could just modify the definition ofindto include the extranrows you bind on for thenivecs.