fork download
  1. # 產生假資料
  2. set.seed(1234)
  3. dt <-
  4. data.frame(
  5. Num = rnorm(15),
  6. Group = c(rep("A", 12), rep("B", 3))
  7. )
  8.  
  9. Num.A <- subset(dt, Group == "A")$Num
  10. Num.B <- subset(dt, Group == "B")$Num
  11.  
  12. res <- vector("list", 4)
  13. # res 是一個用來記錄之後做相關檢驗的容器
  14. # 它本身是一個 list,有 4 個「位子」
  15.  
  16. for(i in 1:4){
  17. Num.A.simulated <- sample(Num.A, 3)
  18. res[[i]] <- cor.test(Num.A.simulated, Num.B)
  19. }
  20.  
  21. res # 所有的結果
  22. res[[1]] # 第一次模擬的結果
  23. sapply(res, function(x){x$estimate}) # 所有的 correlation coefficient
  24. sapply(res, function(x){x$p.value}) # 所有的 p-value
  25.  
Success #stdin #stdout 0.22s 40712KB
stdin
Standard input is empty
stdout
[[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