我有以下矩阵:
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)
型
然而,结果不符合预期的分布。我不想在循环/应用中这样做,因为我的矩阵可以有很多行。
这个命令有什么问题?
2条答案
按热度按时间v2g6jxz61#
您可以使用
apply
函数来实现:字符串
1
表示你想为每一行使用一个函数,然后你必须指定你想为每一行执行的函数。在你的例子中,它是sample
。之后,你必须指定你的示例函数的参数。x759pob22#
字符串