R Bin列并通过随机样本聚合数据,替换迭代较大的Bin大小

R Bin列并通过随机样本聚合数据,替换迭代较大的Bin大小,r,loops,aggregate,lapply,R,Loops,Aggregate,Lapply,下面是一个示例矩阵: mat<- matrix(c(1,0,0,0,0,0,1,0,0,0,0,0,0,0,2,0, 2,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0, 0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0, 0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,1,0,0,1,0,1,1,0,0,1,0,1, 1,1,0,0,0,0,0,0,1,0,1,2,1,0,0,0), nrow=16

下面是一个示例矩阵:

mat<- matrix(c(1,0,0,0,0,0,1,0,0,0,0,0,0,0,2,0,
   2,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,
   0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,
   0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,
   0,0,0,0,1,0,0,1,0,1,1,0,0,1,0,1,
   1,1,0,0,0,0,0,0,1,0,1,2,1,0,0,0), nrow=16, ncol=6)
dimnames(mat)<- list(c("a", "c", "f", "h", "i", "j", "l", "m", "p", "q", "s", "t", "u", "v","x", "z"), 
          c("1", "2", "3", "4", "5", "6"))

我需要帮助修改这行代码,以便在不替换I相邻列的情况下对n个样本随机采样,并使用行和聚合每个样本。请注意,不能对列的组合进行重采样,但如果列是新组合的一部分,则可以对其进行重采样。

这里有一种参数化方法,可以从可能的组合中采样,而不进行替换,并根据原始数据计算摘要,并标记结果列,以便您可以查看它们的来源(并且要有信心,不要重蹈覆辙)

为方便起见,我们可以将其放在函数中:

foo = function(mat, n_cols_in_bin, n_samps) {
  starting_cols = sample(1:(ncol(mat) -  (n_cols_in_bin - 1)), size = n_samps)
  result = sapply(starting_cols, function(x)
    rowSums(mat[, x:(x + n_cols_in_bin - 1)]))
  colnames(result) = paste0("cols", starting_cols, "to", starting_cols + n_cols_in_bin - 1)
  result
}

foo(mat, n_cols_in_bin = 3, n_samps = 2)
#   cols3to5 cols4to6
# a        0        1
# c        1        2
# f        1        0
# h        1        0
# i        2        1
# j        1        1
# l        0        0
# m        1        1
# p        0        1
# q        1        1
# s        1        2
# t        0        2
# u        0        1
# v        1        1
# x        0        0
# z        1        1

感谢您提供此解决方案。我特别感谢您提供了列名称来指定组合的列。但是,此解决方案的问题在于,它无法解决在大型数据集上迭代的问题。在我的实际数据中,我有600列。因此,bin大小可以在2-600之间。在这种情况下,我希望代码通过gh从尺寸2到n的箱子尺寸的原始矩阵,并为这些不同尺寸的箱子随机聚集x个装箱样本。理想情况下,对于每个箱子尺寸,我不必单独指出箱子尺寸的参数。这是我答案的简单扩展。
results=sapply(2:100,foo,mat=mat,n_samps=5)
对于大小为2到100的箱子,每个箱子取5个样本。您只需为循环(或
sapply
循环,或其他任何循环)编写一个
围绕函数。您只需要修改样本在高仓位大小下不可能存在的情况。也就是说,如果您有600列,则只有一个可能的样本包含600个连续列…因此,由于声明的要求是样本不能重复,
foo
如果您要求的独立样本多于现有样本,则会给出错误。好的,谢谢。还有一个问题。您在前面的评论中提出的解决方案输出一个结果矩阵,其中列表示bin大小,并添加行以增加样本。我如何输出结果矩阵列表?每个结果矩阵对于特定bin大小是唯一的,这些矩阵的列表示该大小的样本(在函数中保留命名约定)?为每个箱子大小提供一个唯一的矩阵可以允许不同的尺寸。为增加箱子大小而抽取的样本数量较少。对于列表结果,请将
sapply
更改为
lapply
l
代表
列表
)。如果您想同时改变
n_samps
n_cols\u to_bin
,那么使用
Map
更好,例如
Map(函数(bin,samp)foo(mat,bin,samp),bin=c(2,5100),samp=c(100,100,5))
分别从2,5和100个箱子中获取100,100和5个样本。
set.seed(47)
n_cols_in_bin = 2
n_samps = 4

starting_cols = sample(1:(ncol(mat) -  (n_cols_in_bin - 1)), size = n_samps) 
result = sapply(starting_cols, function(x) rowSums(mat[, x:(x + n_cols_in_bin - 1)]))
colnames(result) = paste0("cols", starting_cols, "to", starting_cols + n_cols_in_bin - 1)
result
#   cols5to6 cols2to3 cols3to4 cols4to5
# a        1        2        0        0
# c        1        0        1        1
# f        0        1        1        0
# h        0        1        1        0
# i        1        2        1        1
# j        0        0        1        1
# l        0        0        0        0
# m        1        0        0        1
# p        1        0        0        0
# q        1        0        0        1
# s        2        0        0        1
# t        2        0        0        0
# u        1        0        0        0
# v        1        0        0        1
# x        0        1        0        0
# z        1        0        0        1
foo = function(mat, n_cols_in_bin, n_samps) {
  starting_cols = sample(1:(ncol(mat) -  (n_cols_in_bin - 1)), size = n_samps)
  result = sapply(starting_cols, function(x)
    rowSums(mat[, x:(x + n_cols_in_bin - 1)]))
  colnames(result) = paste0("cols", starting_cols, "to", starting_cols + n_cols_in_bin - 1)
  result
}

foo(mat, n_cols_in_bin = 3, n_samps = 2)
#   cols3to5 cols4to6
# a        0        1
# c        1        2
# f        1        0
# h        1        0
# i        2        1
# j        1        1
# l        0        0
# m        1        1
# p        0        1
# q        1        1
# s        1        2
# t        0        2
# u        0        1
# v        1        1
# x        0        0
# z        1        1