R语言 按行从矩阵中取样

e3bfsja2  于 2023-11-14  发布在  其他
关注(0)|答案(2)|浏览(179)

我有以下矩阵:

Mat1 <- structure(c("Procedure_B", "Procedure_C", "Procedure_B", NA, 
"Procedure_B", "Procedure_A", "Procedure_C", "Procedure_B", NA, 
"Procedure_B", NA, "Procedure_B", NA, NA, "Procedure_A", "Procedure_A", 
"Procedure_C", "Procedure_A", "Procedure_A", "Procedure_B", "Procedure_C", 
"Procedure_C", "Procedure_C", "Procedure_B", "Procedure_A", "Procedure_A", 
NA, NA, "Procedure_C", NA, "Procedure_C", NA, "Procedure_A", 
"Procedure_B", "Procedure_A", "Procedure_A", "Procedure_A", "Procedure_B", 
"Procedure_A", "Procedure_B", "Procedure_C", "Procedure_B", "Procedure_B", 
"Procedure_B", "Procedure_C", "Procedure_C", "Procedure_A", NA, 
NA, NA, NA, NA, NA, "Procedure_A", "Procedure_A", "Procedure_A", 
"Procedure_A", "Procedure_A", "Procedure_A", "Procedure_A", "Procedure_A", 
"Procedure_A", "Procedure_A", "Procedure_A", "Procedure_A", "Procedure_A", 
"Procedure_A", "Procedure_A", "Procedure_A", "Procedure_A", "Procedure_A", 
"Procedure_A", "Procedure_A", "Procedure_A", "Procedure_A", "Procedure_A", 
"Procedure_A", "Procedure_A", "Procedure_A", "Procedure_A", "Procedure_A", 
"Procedure_A", "Procedure_A", "Procedure_A", "Procedure_A", "Procedure_A", 
"Procedure_A", "Procedure_A", "Procedure_A", "Procedure_A", "Procedure_A", 
"Procedure_A", "Procedure_A", "Procedure_A", "Procedure_A", "Procedure_A", 
"Procedure_A", "Procedure_A", "Procedure_A", "Procedure_A", "Procedure_A", 
"Procedure_A", "Procedure_A", "Procedure_A", "Procedure_A", "Procedure_A", 
"Procedure_B", "Procedure_B", "Procedure_B", "Procedure_B", "Procedure_B", 
"Procedure_B", "Procedure_B", "Procedure_B", "Procedure_B", "Procedure_B", 
"Procedure_B", "Procedure_B", "Procedure_B", "Procedure_B", "Procedure_B", 
"Procedure_B", "Procedure_B", "Procedure_B", "Procedure_B", "Procedure_B", 
"Procedure_B", "Procedure_B", "Procedure_B", "Procedure_B", "Procedure_B", 
"Procedure_B", "Procedure_B", "Procedure_B", "Procedure_B", "Procedure_B", 
"Procedure_B", "Procedure_B", "Procedure_B", "Procedure_B", "Procedure_B", 
"Procedure_B", "Procedure_B", "Procedure_B", "Procedure_B", "Procedure_B", 
"Procedure_B", "Procedure_B", "Procedure_B", "Procedure_B", "Procedure_B", 
"Procedure_B", "Procedure_B", "Procedure_B", "Procedure_B", "Procedure_B", 
"Procedure_B", "Procedure_B", "Procedure_B", "Procedure_C", "Procedure_C", 
"Procedure_C", "Procedure_C", "Procedure_C", "Procedure_C", "Procedure_C", 
"Procedure_C", "Procedure_C", "Procedure_C", "Procedure_C", "Procedure_C", 
"Procedure_C", "Procedure_C", "Procedure_C", "Procedure_C", "Procedure_C", 
"Procedure_C", "Procedure_C", "Procedure_C", "Procedure_C", "Procedure_C", 
"Procedure_C", "Procedure_C", "Procedure_C", "Procedure_C", "Procedure_C", 
"Procedure_C", "Procedure_C", "Procedure_C", "Procedure_C", "Procedure_C", 
"Procedure_C", "Procedure_C", "Procedure_C", "Procedure_C", "Procedure_C", 
"Procedure_C", "Procedure_C", "Procedure_C", "Procedure_C", "Procedure_C", 
"Procedure_C", "Procedure_C", "Procedure_C", "Procedure_C", "Procedure_C", 
"Procedure_C", "Procedure_C", "Procedure_C", "Procedure_C", "Procedure_C", 
"Procedure_C", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA), dim = c(53L, 5L))

字符串
我想从每一行中抽取一个值,具有以下常量概率:

P = c(0.99, 0.002992, 0.003186, 0.003018, 0.000804)


即,对于每行样本,其5个值中的每一个具有这些概率
预期输出为53个值。
我试过了:

sample(Mat1, size = nrow(Mat1), prob = rep(P, nrow(Mat1)), replace = T)


然而,结果不符合预期的分布。我不想在循环/应用中这样做,因为我的矩阵可以有很多行。
这个命令有什么问题?

v2g6jxz6

v2g6jxz61#

您可以使用apply函数来实现:

apply(Mat1, 1, sample, prob=c(0.5, 0.2412, 0.2397, 0.0191), size=1)

字符串
1表示你想为每一行使用一个函数,然后你必须指定你想为每一行执行的函数。在你的例子中,它是sample。之后,你必须指定你的示例函数的参数。

x759pob2

x759pob22#

  • 您可以对列索引进行采样,并根据采样的列编号从矩阵中提取值。
  • 该方法是矢量化的。
set.seed(1)

cols <- sample(1:ncol(Mat1), size = nrow(Mat1), replace = TRUE, prob = P)
Mat1[cbind(1:nrow(Mat1), cols)]

#  [1] "Procedure_B" "Procedure_C" "Procedure_B" NA            "Procedure_B"
#  [6] "Procedure_A" "Procedure_C" "Procedure_B" NA            "Procedure_B"
# [11] NA            "Procedure_B" NA            NA            "Procedure_A"
# [16] "Procedure_A" "Procedure_C" "Procedure_B" "Procedure_A" "Procedure_B"
# [21] "Procedure_C" "Procedure_C" "Procedure_C" "Procedure_B" "Procedure_A"
# [26] "Procedure_A" NA            NA            "Procedure_C" NA           
# [31] "Procedure_C" NA            "Procedure_A" "Procedure_B" "Procedure_A"
# [36] "Procedure_A" "Procedure_A" "Procedure_B" "Procedure_A" "Procedure_B"
# [41] "Procedure_C" "Procedure_B" "Procedure_B" "Procedure_B" "Procedure_C"
# [46] "Procedure_C" "Procedure_A" NA            NA            NA           
# [51] NA            NA            NA

字符串

相关问题