采样分布和表的总和

时间:2016-03-05 18:48:45

标签: r statistics

我做了一些实验,每个实验都导致了颜色的出现。 由于我无法进行更多实验,我希望通过sample size=30查看我可以获得1000次采样的频率表(颜色)。得到的频率表应该是1000频率表的总和。

我认为如下连接表并尝试聚合,但它不起作用:

 mydata=structure(list(Date = structure(c(11L, 1L, 9L, 9L, 10L, 1L, 2L, 
3L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 6L, 7L, 4L, 4L, 4L, 6L, 6L, 11L, 
5L, 4L, 7L, 10L, 6L, 6L, 2L, 5L, 7L, 11L, 1L, 9L, 11L, 11L, 11L, 
1L, 1L), .Label = c("01/02/2016", "02/02/2016", "03/02/2016", 
"08/02/2016", "10/02/2016", "11/02/2016", "16/02/2016", "22/02/2016", 
"26/01/2016", "27/01/2016", "28/01/2016"), class = "factor"), 
    Color = structure(c(30L, 33L, 11L, 1L, 18L, 18L, 11L, 
    16L, 19L, 19L, 22L, 1L, 18L, 18L, 13L, 14L, 13L, 18L, 24L, 
    24L, 11L, 24L, 2L, 33L, 25L, 1L, 30L, 5L, 24L, 18L, 13L, 
    35L, 19L, 19L, 18L, 23L, 19L, 8L, 19L, 14L), .Label = c("ARD", 
    "ARP", "BBB", "BIE", "CFX", "CHR", "DDD", "DOO", "EAU", "ELY", 
    "EPI", "ETR", "GEN", "GER", "GGG", "GIS", "ISE", "JUV", "LER", 
    "LES", "LON", "LYR", "MON", "NER", "NGY", "NOJ", "NYO", "ORI", 
    "PEO", "RAY", "RRR", "RSI", "SEI", "SEP", "VIL", "XQU", "YYY", 
    "ZYZ"), class = "factor"), Categorie = structure(c(1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1", "1,2", "1,2,3", 
    "1,3", "2", "2,3", "3", "4", "5"), class = "factor"), Portion_Longueur = c(3L, 
    4L, 1L, 1L, 2L, 4L, 5L, 6L, 7L, 7L, 8L, 8L, 9L, 8L, 8L, 9L, 
    11L, 7L, 7L, 7L, 9L, 8L, 3L, 8L, 7L, 11L, 2L, 9L, 8L, 5L, 
    8L, 12L, 3L, 4L, 1L, 3L, 3L, 3L, 4L, 5L)), .Names = c("Date", 
"Color", "Categorie", "Portion_Longueur"), row.names = c(NA, 
40L), class = "data.frame")

for (i  in 1:1000) {
mysamp= sample(mydata$Color,size=30)
x=data.frame(table(mysamp))

if (i==1) w=x
else w <- c(w, x)

}
aggregate(w$Freq, by=list(Color=w$mysamp), FUN=sum)

示例,对于3次采样,for (i in 1:3)我希望总和如下: enter image description here

但我没有Sum,而是我:

Color x
1    ARD 2
2    ARP 1
3    BBB 0
4    BIE 0
5    CFX 0
6    CHR 0
7    DDD 0
8    DOO 1
9    EAU 0
10   ELY 0
11   EPI 3
12   ETR 0
13   GEN 2
14   GER 2
15   GGG 0
16   GIS 1
17   ISE 0
18   JUV 4
19   LER 5
20   LES 0
21   LON 0
22   LYR 1
23   MON 1
24   NER 2
25   NGY 1
26   NOJ 0
27   NYO 0
28   ORI 0
29   PEO 0
30   RAY 1
31   RRR 0
32   RSI 0
33   SEI 2
34   SEP 0
35   VIL 1
36   XQU 0
37   YYY 0
38   ZYZ 0

怎么做?

非常感谢

1 个答案:

答案 0 :(得分:2)

您的for循环导致了您的问题。您最终会创建一个很难执行计算的大列表(请查看names(w)以查看我的意思)。更好的数据结构可以更容易地进行计算:

x = NULL #initialize
for (i  in 1:1000) {
    mysamp = sample(mydata$Color,size=30) #sample
    mysamp = data.frame(table(mysamp)) #frequency
    x = rbind(x, mysamp) #bind to x
}
aggregate(Freq~mysamp, data = x, FUN = sum) #perform calculation

请注意,此循环运行速度比循环慢一点。这是因为rbind()功能。见post。也许有人会提出更有效的解决方案。