我正在运行彩票风格草稿的模拟,它创建了一个x#可能订单的矩阵。列是选择(例如,第一列是选择1,第二列是选择2等。)我试图计算每个团队通过计算列来获取每个选择的次数。但是,使用table
函数我可以显示值,但无法弄清楚如何将它们存储到矩阵/向量中。这是我目前拥有的代码的子集。
tickets = c("A","B","B","C","C","C","D","D","D","D")#Population to draw
df <- matrix(data=NA,nrow=100,ncol=4)# row = #trials, col = # teams
for ( i in 1:100)
#block of code to run
{
order= c(sample(tickets,10,FALSE,))
temp = unique(order)
df[i,] = temp
} #draw all the tickets and delete duplicate values
one = table(df[,1]) #count number times each team gets 1st pick
one # display
A B C D
12 18 26 44
我想将每列的值放入向量中,然后放入矩阵中以运行概率。
答案 0 :(得分:1)
您可以按列split
(因为它是一个矩阵,否则如果它是data.frame
或列表,只有sapply
),那么列表
sapply(split(df, col(df)), table)
# 1 2 3 4
# A 10 12 23 55
# B 19 27 32 22
# C 32 28 21 19
# D 39 33 24 4