# 產生假資料
set.seed(1234)
dt <-
data.frame(
Num = rnorm(15),
Group = c(rep("A", 12), rep("B", 3))
)
Num.A <- subset(dt, Group == "A")$Num
Num.B <- subset(dt, Group == "B")$Num
res <- vector("list", 4)
# res 是一個用來記錄之後做相關檢驗的容器
# 它本身是一個 list,有 4 個「位子」
for(i in 1:4){
Num.A.simulated <- sample(Num.A, 3)
res[[i]] <- cor.test(Num.A.simulated, Num.B)
}
res # 所有的結果
res[[1]] # 第一次模擬的結果
sapply(res, function(x){x$estimate}) # 所有的 correlation coefficient
sapply(res, function(x){x$p.value}) # 所有的 p-value
IyDnlKLnlJ/lgYfos4fmlpkKc2V0LnNlZWQoMTIzNCkKZHQgPC0KICBkYXRhLmZyYW1lKAogICAgTnVtID0gcm5vcm0oMTUpLAogICAgR3JvdXAgPSBjKHJlcCgiQSIsIDEyKSwgcmVwKCJCIiwgMykpCiAgKQoKTnVtLkEgPC0gc3Vic2V0KGR0LCBHcm91cCA9PSAiQSIpJE51bQpOdW0uQiA8LSBzdWJzZXQoZHQsIEdyb3VwID09ICJCIikkTnVtCgpyZXMgPC0gdmVjdG9yKCJsaXN0IiwgNCkKIyByZXMg5piv5LiA5YCL55So5L6G6KiY6YyE5LmL5b6M5YGa55u46Zec5qqi6amX55qE5a655ZmoCiMg5a6D5pys6Lqr5piv5LiA5YCLIGxpc3TvvIzmnIkgNCDlgIvjgIzkvY3lrZDjgI0KCmZvcihpIGluIDE6NCl7CiAgTnVtLkEuc2ltdWxhdGVkIDwtIHNhbXBsZShOdW0uQSwgMykKICByZXNbW2ldXSA8LSBjb3IudGVzdChOdW0uQS5zaW11bGF0ZWQsIE51bS5CKQp9CgpyZXMgIyDmiYDmnInnmoTntZDmnpwKcmVzW1sxXV0gIyDnrKzkuIDmrKHmqKHmk6znmoTntZDmnpwKc2FwcGx5KHJlcywgZnVuY3Rpb24oeCl7eCRlc3RpbWF0ZX0pICMg5omA5pyJ55qEIGNvcnJlbGF0aW9uIGNvZWZmaWNpZW50CnNhcHBseShyZXMsIGZ1bmN0aW9uKHgpe3gkcC52YWx1ZX0pICMg5omA5pyJ55qEIHAtdmFsdWUK
[[1]]
Pearson's product-moment correlation
data: Num.A.simulated and Num.B
t = -1.2788, df = 1, p-value = 0.4225
alternative hypothesis: true correlation is not equal to 0
sample estimates:
cor
-0.7877348
[[2]]
Pearson's product-moment correlation
data: Num.A.simulated and Num.B
t = 0.010969, df = 1, p-value = 0.993
alternative hypothesis: true correlation is not equal to 0
sample estimates:
cor
0.01096878
[[3]]
Pearson's product-moment correlation
data: Num.A.simulated and Num.B
t = -1.5004, df = 1, p-value = 0.3743
alternative hypothesis: true correlation is not equal to 0
sample estimates:
cor
-0.8321147
[[4]]
Pearson's product-moment correlation
data: Num.A.simulated and Num.B
t = 3.0829, df = 1, p-value = 0.1997
alternative hypothesis: true correlation is not equal to 0
sample estimates:
cor
0.9512099
Pearson's product-moment correlation
data: Num.A.simulated and Num.B
t = -1.2788, df = 1, p-value = 0.4225
alternative hypothesis: true correlation is not equal to 0
sample estimates:
cor
-0.7877348
cor cor cor cor
-0.78773478 0.01096878 -0.83211471 0.95120986
[1] 0.4225075 0.9930169 0.3742602 0.1996839