添加向量后从现有矩阵中随机选择值(在 R 中)
非常感谢您提前提供的帮助!
我正在尝试修改现有矩阵,以便当向矩阵添加新行时,它会从预先存在的矩阵中删除值。
例如,我有一个矩阵:
[,1] [,2] [,3] [,4]
1 1 0 0
0 1 0 0
1 0 1 0
0 0 1 1
我想添加另一个向量 I.vec,它有两个值 (I.vec=c(0,1,1,0)
)。 这很容易做到。我只是将它绑定到矩阵。 现在,对于 I.vec 等于 1 的每一列,我想从其他行中随机选择一个值并将其设为零。 理想情况下,这最终会得到一个如下矩阵:
[,1] [,2] [,3] [,4]
1 0 0 0
0 1 0 0
1 0 0 0
0 0 1 1
0 1 1 0
但每次运行迭代时,我希望它再次随机采样。
这就是我尝试过的:
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
但这只会删除我想删除的两个值之一。我也尝试过对矩阵进行子集化的变体,但没有成功。
再次感谢您!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
OP 的描述有点含糊不清,因此建议两种解决方案:
假设只有相关列中现有的
1
可以设置为0
I'只需更改原始功能(见下文)。更改是针对定义行的行。我现在有(原始版本中有一个错误 - 下面的版本经过修改以处理该错误):
基本上,它的作用是,对于每一列,我们需要将
1
交换为0
,我们计算出该列的哪些行包含1
,并对其中之一进行采样。编辑:我们必须处理列中只有一个
1
的情况。如果我们只是从长度为 1 的向量中采样,R 的sample()
会将其视为我们想要从集合seq_len(n)
中采样,而不是从长度为 1 的集合中采样n
。我们现在使用if, else
语句来处理这个问题。我们必须对每一列单独执行此操作,以便获得正确的行。我想我们可以做一些很好的操作来避免重复调用
which()
和sample()
,但目前我无法理解,因为我们确实必须处理列中只有一个1
的情况。这是完成的函数(已更新以处理原始函数中长度为 1 的示例错误):它正在运行:
当我们想要进行交换的列中只有一个
1
时,它会起作用 原始答案:假设相关列中的任何值都可以设置为零
这是一个向量化答案,我们在进行替换时将矩阵视为向量。使用示例数据:
这给了我们:
如果我这样做不止一次或两次,我会将其包装在一个函数中:
这给出了:
如果您想对多个
ivec
执行此操作,每次都增长mat1
,那么您可能不想在循环中执行此操作,因为增长的对象很慢(它涉及副本等)。但是您可以修改ind
的定义以包含为n
ivec
绑定的额外n
行s。There is a little bit of ambiguity in the description from the OP, so two solutions are suggested:
Assuming that only existing
1
s in relevant columns can be set to0
I'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
1
to a0
, we work out which rows of the column contain1
s and sample one of these.Edit: We have to handle the case where there is only a single
1
in 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, else
statement.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 one1
in 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
1
in 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
ivec
s, growingmat1
each 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 ofind
to include the extran
rows you bind on for then
ivec
s.你可以尝试这样的事情。在那里添加“nrow”将允许您与其他“I.vec”一起运行多次。我尝试用“apply”在一行中完成此操作,但无法再次得到矩阵。
You could try something like this. Having 'nrow' in there will allow you to run it multiple times with other 'I.vec's. I tried to do this in a single line with 'apply' but couldn't get a matrix to come out again.