r - 将sample()的输出与R中的rbinom()相匹配?

标签 r random sampling resampling

也许这个问题太简单了,但在我下面的R代码中,AB不应该完全相等:

set.seed(0)
A <- sample(0:1, 50, prob = rep(.5, 2), replace = T) # 'A' is HERE
plot(table(A))

set.seed(0)
B <- rbinom(50, 1, .5)  # 'B' is HERE
plot(table(B))

最佳答案

如果我们取“B”的否定,它会是相同的,即如果我们查看“B”的输出,它有 0 个值,其位置与 A 中的 1 的位置相同,反之亦然

identical(A, +!B)
#[1] TRUE

或者不进行求反,而是通过相反的操作(即 1:0)创建“A”

set.seed(0)
A <- sample(1:0, 50, prob = rep(.5, 2), replace = TRUE)
identical(A, B)
#[1] TRUE

关于r - 将sample()的输出与R中的rbinom()相匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50669921/

相关文章:

html - R Shiny withProgress Indicator Within Fluid Container

使用不同参数调用函数会产生相同的输出。 (C)

python - 如何在Python中使用random.sample()后获取剩余的样本?

audio - 混合多个声音片段

r - 在 R 中将 `rnorm` 作为另一个 `rnorm` 的参数意味着什么?

r - 在 r 中绘制没有 google map API 的城市 map

r - Rcpp 中列表的索引元素

python - numpy 3d 数组 -- 展平 --> 1d 数组 --> 选择 1d 中的一个元素 --> 如何知道该元素在 3d 中的索引?

r - 从 HTML 中抓取 pdf 文件

java.util.Random 特性