非常感谢你的帮助!
我正在尝试修改现有矩阵,以便在将新行添加到矩阵时,它会从预先存在的矩阵中删除值.
例如,我有矩阵:
[,1] [,2] [,3] [,4]
1 1 0 0
0 1 0 0
1 0 1 0
0 0 1 1
Run Code Online (Sandbox Code Playgroud)
我想添加另一个矢量,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
Run Code Online (Sandbox Code Playgroud)
但是每次运行迭代时,我都希望它再次随机采样.
所以这就是我尝试过的:
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
Run Code Online (Sandbox Code Playgroud)
但这只删除了我想要删除的两个值中的一个.我也尝试过对矩阵进行子集化的变化,但我还没有成功.
再次感谢你!
OP的描述有一点歧义,因此建议两种解决方案:
1相关列中存在的s 可以设置为0我只会改变原来的功能(见下文)。更改是针对定义rows. 我现在有(原来有一个错误 - 下面的版本被修改以处理错误):
rows <- sapply(seq_along(cols),
function(x, mat, cols) {
ones <- which(mat[,cols[x]] == 1L)
out <- if(length(ones) == 1L) {
ones
} else {
sample(ones, 1)
}
out
}, mat = mat, cols = cols)
Run Code Online (Sandbox Code Playgroud)
基本上,这样做的作用是,对于我们需要将 a 交换1为 a 的每一列0,我们计算出该列的哪些行包含1s 并对其中的一个进行采样。
编辑:我们必须处理1一列中只有一个的情况。如果我们只是从长度为 1 的向量中采样,Rsample()会将其视为我们想要从集合中seq_len(n)而不是从长度为 1 的集合中进行采样n。我们现在用一个if, else语句来处理这个问题。
我们必须对每一列单独执行此操作,以便获得正确的行。我想我们可以做一些很好的操作来避免重复调用which()and sample(),但是现在如何逃避我,因为我们必须处理1列中只有一个的情况。这是完成的函数(已更新以处理原始长度为 1 的示例错误):
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)
out <- if(length(ones) == 1L) {
ones
} else {
sample(ones, 1)
}
out
}, mat = mat, cols = cols)
ind <- (nr*(cols-1)) + rows
mat[ind] <- 0
mat <- rbind(mat, vec)
rownames(mat) <- NULL
mat
}
Run Code Online (Sandbox Code Playgroud)
这是在行动:
> set.seed(2)
> foo(mat1, ivec)
[,1] [,2] [,3] [,4]
[1,] 1 0 0 0
[2,] 0 1 0 0
[3,] 1 0 1 0
[4,] 0 0 0 1
[5,] 0 1 1 0
Run Code Online (Sandbox Code Playgroud)
当1我们想要交换的列中只有一个时,它会起作用:
> foo(mat1, c(0,0,1,1))
[,1] [,2] [,3] [,4]
[1,] 1 1 0 0
[2,] 0 1 0 0
[3,] 1 0 1 0
[4,] 0 0 0 1
[5,] 0 0 1 1
Run Code Online (Sandbox Code Playgroud)
这是一个向量化的答案,我们在进行替换时将矩阵视为向量。使用示例数据:
mat1 <- matrix(c(1,1,0,0,0,1,0,0,1,0,1,0,0,0,1,1), byrow = TRUE, nrow = 4)
ivec <- c(0,1,1,0)
## Set a seed to make reproducible
set.seed(2)
## number of rows and columns of our matrix
nr <- nrow(mat1)
nc <- ncol(mat1)
## which of ivec are 1L
cols <- which(ivec == 1L)
## sample length(cols) row indices, with replacement
## so same row can be drawn more than once
rows <- sample(seq_len(nr), length(cols), replace = TRUE)
## Compute the index of each rows cols combination
## if we treated mat1 as a vector
ind <- (nr*(cols-1)) + rows
## ind should be of length length(cols)
## copy for illustration
mat2 <- mat1
## replace the indices we want with 0, note sub-setting as a vector
mat2[ind] <- 0
## bind on ivec
mat2 <- rbind(mat2, ivec)
Run Code Online (Sandbox Code Playgroud)
这给了我们:
> mat2
[,1] [,2] [,3] [,4]
1 0 0 0
0 1 0 0
1 0 0 0
0 0 1 1
ivec 0 1 1 0
Run Code Online (Sandbox Code Playgroud)
如果我这样做不止一次或两次,我会将它包装在一个函数中:
foo <- function(mat, vec) {
nr <- nrow(mat)
nc <- ncol(mat)
cols <- which(vec == 1L)
rows <- sample(seq_len(nr), length(cols), replace = TRUE)
ind <- (nr*(cols-1)) + rows
mat[ind] <- 0
mat <- rbind(mat, vec)
rownames(mat) <- NULL
mat
}
Run Code Online (Sandbox Code Playgroud)
这使:
> foo(mat1, ivec)
[,1] [,2] [,3] [,4]
[1,] 1 1 0 0
[2,] 0 1 0 0
[3,] 1 0 1 0
[4,] 0 0 0 1
[5,] 0 1 1 0
Run Code Online (Sandbox Code Playgroud)
如果您想对多个ivecs执行此操作,mat1每次都增长,那么您可能不想在循环中这样做,因为增长的对象很慢(它涉及副本等)。但是您可以修改 的定义ind以包含n您为n ivecs绑定的额外行。