Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
选择由R中的随机向量指定的列_R_Matrix_Subset_Random Sample - Fatal编程技术网

选择由R中的随机向量指定的列

选择由R中的随机向量指定的列,r,matrix,subset,random-sample,R,Matrix,Subset,Random Sample,我有一个较大的矩阵,我想从中随机抽取一个较小的矩阵。(我想这样做1000次,所以最终它将在一个for循环中。)例如,我有一个9x9矩阵: mat=matrix(c(0,0,1,0,1,0,0,0,1,0,0,0,0,1,1,1,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,1, 0,0,0,0,1,1,1,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0,0,1,1,1,0,0, 1,0,1,0,0,0,0,

我有一个较大的矩阵,我想从中随机抽取一个较小的矩阵。(我想这样做1000次,所以最终它将在一个for循环中。)例如,我有一个9x9矩阵:

mat=matrix(c(0,0,1,0,1,0,0,0,1,0,0,0,0,1,1,1,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,1,
          0,0,0,0,1,1,1,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0,0,1,1,1,0,0,
          1,0,1,0,0,0,0,0,1,0,1,0,0,0,1), nrow=9)
从这个矩阵中,我想要一个随机的3x3子集。诀窍在于,我不希望最终矩阵中的任何行或列和为0。另一件重要的事情是,我需要知道最终矩阵中的原始行数和列数。因此,如果我最终随机选择第4、5和7行以及第1、3和8列,我希望在最终的矩阵中可以轻松访问这些标识符

这是我到目前为止所做的

首先,我创建一个行号和列号的向量。我一直在努力将这些连接到矩阵

r.num<-seq(from=1,to=nrow(mat),by=1)      #vector of row numbers
c.num<-seq(from=0, to=(ncol(mat)+1),by=1) #vector of col numbers (adj for r.num)

mat.1<-cbind(r.num,mat)
mat.2<-rbind(c.num,mat.1)

r.num如果我理解您的问题,我认为这会起作用:

mat=matrix(c(0,0,1,0,1,0,0,0,1,0,0,0,0,1,1,1,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,1,
          0,0,0,0,1,1,1,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0,0,1,1,1,0,0,
          1,0,1,0,0,0,0,0,1,0,1,0,0,0,1), nrow=9)

smallmatrix = matrix(0,,nrow=3,ncol=3)

 while(any(apply(smallmatrix,2,sum) ==0) | any(apply(smallmatrix,1,sum) ==0)){
      cols = sample(ncol(mat),3)
      rows= sample(nrow(mat),3)
      smallmatrix = mat[rows,cols]
}

colnames(smallmatrix) = cols
rownames(smallmatrix) = rows

最后的1000个子集矩阵是否应该是唯一的?这并不重要。我从中采样的原始矩阵是1174行455列,所以我想得到一个代表性的采样。然而,我确信有有限的可能性。唯一的问题是,如果对其中一个独特的子矩阵存在某种抽样偏差。哈哈,我几乎想发布相同的答案,但你抢先了我!我只认为在计算行和列的总和是否为零时,使用
rowSums
colSums
会更快:
any(colSums(smallmatrix)==0)| any(rowSums(smallmatrix)==0)
Oops,我刚刚注意到有些行使用这种方法得到的结果是零和。还有别的主意吗?我刚修好@萨切普斯卡姆从一开始就做对了。我没有注意到限制也适用于行。所以只是在中间添加了一个or。谢谢!这是非常有帮助的。
rand2 <- sample(c.num,3)
temp2 <- cbind(temp1[,1],temp1[,rand2])
temp3 <- temp1[,which(colSums(temp1[2:nrow(temp1),])>0)]
cols <- which(colSums(temp1[2:nrow(temp1),2:ncol(temp1)])>0)
rand3 <- sample(cols,3)
temp4 <- cbind(temp3[,1],temp3[,rand3])
mat=matrix(c(0,0,1,0,1,0,0,0,1,0,0,0,0,1,1,1,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,1,
          0,0,0,0,1,1,1,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0,0,1,1,1,0,0,
          1,0,1,0,0,0,0,0,1,0,1,0,0,0,1), nrow=9)

smallmatrix = matrix(0,,nrow=3,ncol=3)

 while(any(apply(smallmatrix,2,sum) ==0) | any(apply(smallmatrix,1,sum) ==0)){
      cols = sample(ncol(mat),3)
      rows= sample(nrow(mat),3)
      smallmatrix = mat[rows,cols]
}

colnames(smallmatrix) = cols
rownames(smallmatrix) = rows