回复:从矩阵中随机选择值,添加 for 循环(在 R 中)
这是我上周问题的后续,可以找到此处。我不确定将这个问题发布在同一个地方是否合适,或者将其作为一个新问题发布。
好的,上次我询问如何在将新向量绑定到矩阵后从矩阵中随机删除值。答案非常有用,但是当我使用非方阵时我发现了一个错误。我一直在循环中运行代码并每次对矩阵求和以确保其正常工作,但我发现总和有所不同,这意味着代码有时会在矩阵中选择错误的值(我希望它只选择和替换)。
这是代码:
mat1<-matrix(c(1,0,1,0, 0,1,1,1, 1,0,0,0, 1,0,0,1, 1,1,1,1, 0,0,0,1),byrow=F, nrow=4)
I.vec<-c(0,1,1,1,0,0)
foo <- function(mat, vec) {
nr <- nrow(mat)
nc <- ncol(mat)
cols <- which(vec == 1L)
rows <- sapply(seq_along(cols),
function(x, mat, cols) {
ones <- which(mat[,cols[x]] == 1L)
sample(ones, 1)
}, mat = mat, cols = cols)
ind <- (nr*(cols-1)) + rows
mat[ind] <- 0
mat <- rbind(mat, vec)
rownames(mat) <- NULL
mat
}
set.seed(2)
for (j in 1:1000){ #run this vector through the simulations
I.vec2=sample(I.vec,replace=FALSE) #randomize interactions
temp=foo(mat1,I.vec2) #run foo function
prop=sum(temp)
print.table(prop)
}
在这种情况下,有时矩阵的总和是 13,有时是 14,而它应该始终是 = sum(mat1) = 13。
我试图拆开代码,我认为一切除了 rows 函数之外,它工作正常,不可否认,我并不完全理解。
Possible Duplicate:
Randomly selecting values from an existing matrix after adding a vector (in R)
This is a follow up to my question from last week and can be found here. I wasn't sure if it was appropriate to post this question in the same place, or to post it as a new question.
Okay, last time I asked about randomly removing values from a matrix after binding a new vector to it. The answers were very useful, but I have found a bug when I am using a non square matrix. I have been running the code in a loop and taking the sum of the matrix each time to ensure that it is working properly, but I have found that the sum varies, which would imply that the code is sometimes selecting the wrong value in the matrix (I want it to only select and replace ones).
Here is the code:
mat1<-matrix(c(1,0,1,0, 0,1,1,1, 1,0,0,0, 1,0,0,1, 1,1,1,1, 0,0,0,1),byrow=F, nrow=4)
I.vec<-c(0,1,1,1,0,0)
foo <- function(mat, vec) {
nr <- nrow(mat)
nc <- ncol(mat)
cols <- which(vec == 1L)
rows <- sapply(seq_along(cols),
function(x, mat, cols) {
ones <- which(mat[,cols[x]] == 1L)
sample(ones, 1)
}, mat = mat, cols = cols)
ind <- (nr*(cols-1)) + rows
mat[ind] <- 0
mat <- rbind(mat, vec)
rownames(mat) <- NULL
mat
}
set.seed(2)
for (j in 1:1000){ #run this vector through the simulations
I.vec2=sample(I.vec,replace=FALSE) #randomize interactions
temp=foo(mat1,I.vec2) #run foo function
prop=sum(temp)
print.table(prop)
}
In this case, sometimes the sum of the matrix is 13 and sometimes it is 14, when it should always be = sum(mat1) = 13.
I've tried to pick apart the code, and I think everything is working correctly except for the rows function, which, admittedly, I do not fully understand.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该问题是
sample()
的一个功能。我将更新原始 Q,但问题是由于在候选矩阵的列中观察到单个1
造成的。生成rows
的代码试图从一组 1 中进行采样。不幸的是,我忘记了sample()
有一个功能,当第一个参数是长度向量时1、sample()
将其视为您想要从集合1
, ...,n
中采样,其中n< /code> 是您真正想要从中采样的集合中单个元素的值。
一个简单的示例说明了当
sample()
的参数x
是长度为 1 的向量时的行为:本能地,这些都应该是
4
,但是它们不是因为这个已记录且众所周知的功能。The problem is a feature of
sample()
. I will update the original Q, but the problem is due to a single1
being observed in a column in the candidate matrix. The code that generatesrows
is trying to sample from a set of 1. Unfortunately, I forgot thatsample()
has a feature that when the first argument is a vector of length 1,sample()
treats it as if you wanted to sample from the set1
, ...,n
wheren
was the value of the single element in the set youe really wanted to sample from.A simple example illustrates the behaviour when the argument
x
tosample()
is a length 1 vector:Instinctively, these should all be
4
, but they aren't because of this documented and well known feature.