0

我有一个轮盘模拟,可以绘制频率与轮盘插槽(或因子)的关系,但我也想查看相对频率与因子的百分比。

black_on_wheel = paste("B", 1:18, sep = "") 
red_on_wheel = paste("R", 1:18, sep = "")
roulette_wheel = c(red_on_wheel, black_on_wheel, "0", "00")
simulated_roulette_wheel = sample(roulette_wheel, size=500, replace = TRUE)
plot(rw_runs)
4

2 回答 2

0
rw_runs <- table( simulated_roulette_wheel)
str(rw_runs)
# 'table' int [1:38(1d)] 19 9 13 8 19 16 12 11 14 13 ...
# - attr(*, "dimnames")=List of 1
#  ..$ simulated_roulette_wheel: chr [1:38] "0" "00" "B1" "B10" ...
 barplot( rw_runs*100/sum(rw_runs) )
于 2012-09-19T03:22:03.643 回答
0

正如@joran 指出的那样,您可以使用tableand prop.table

set.seed(001) # For the simulation to be reproducible.
simulated_roulette_wheel = sample(roulette_wheel, size=500, replace = TRUE)

tab <-table(simulated_roulette_wheel)                  # Frequency of each factor
prop.tab <- prop.table(tab) * 100                      # % Relative Freq.
barplot(prop.tab, xaxs='i', ylab="Relative %") ; box() # Barplot

barplot,xaxs="i"允许条形图从 x 坐标的原点开始,并且该函数box()在图中添加一个框。

的前十个元素prop.tab如下所示:

prop.tab[1:10]
simulated_roulette_wheel
  0  00  B1 B10 B11 B12 B13 B14 B15 B16 
2.4 2.8 4.6 3.4 2.2 3.0 1.8 2.4 2.2 2.2 

如果你不乘以prop.table(tab)100,那么你只会得到一个比例而不是相对百分比。

这是生成的条形图:

在此处输入图像描述

于 2012-09-19T06:58:24.590 回答