计算 R 中矩阵列中的值

count values in matrix column in R

我正在 运行模拟彩票式草稿,它创建了一个包含 x# 个可能订单的矩阵。列是选秀权(例如,第一列是选秀权 1,第二列是选秀权 2 等。)我试图通过计算列数来计算每个团队获得每个选秀权的次数。但是使用 table 函数我可以显示这些值,但无法弄清楚如何将它们存储到 matrix/vector 中。这是我目前拥有的代码的子集。

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 

我想将每一列的值放入向量中,然后放入矩阵中以获得 运行 概率。

您可以 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