将双向 table() 转换为 R 中的成对计数列表 [英] Transforming a two-way table() into pairwise list of counts in R
本文介绍了将双向 table() 转换为 R 中的成对计数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从一些示例双向频率表开始:
Starting with some sample two-way frequency table:
a <- c(1,2,3,4,4,3,4,2,2,2)
b <- c(1,2,3,4,1,2,4,3,2,2)
tab <- table(a,b)
> tab
b
a 1 2 3 4
1 1 0 0 0
2 0 3 1 0
3 0 1 1 0
4 1 0 0 2
我需要将表格转换成以下格式:
I need to transform the table into the following format:
goal <- data.frame(a=c(1,2,3,4),b=c(1,2,3,4),count=c(1,3,1,2))
> goal
a b count
1 1 1 1
2 2 2 3
3 3 3 1
4 4 4 2
. . . .
如何从双向表中形成所有成对组合并在第三列中添加频率计数?
How can I form all pairwise combinations from the two-way table and add the frequency counts in the third column?
直觉告诉我,table
应该有一种简单的反向"函数,但我在 SO 或 Google 上找不到任何东西.
Intuition tells me there should be a simple kind of 'reverse' function for table
, but I could not find anything on SO or Google.
推荐答案
自然,在发布问题后,我找到了正确的 Google 搜索查询...
Naturally, after posting the question I found the right search query for Google...
> as.data.frame(tab)
a b Freq
1 1 1 1
2 2 1 0
3 3 1 0
4 4 1 1
5 1 2 0
6 2 2 3
7 3 2 1
8 4 2 0
9 1 3 0
10 2 3 1
11 3 3 1
12 4 3 0
13 1 4 0
14 2 4 0
15 3 4 0
16 4 4 2
这篇关于将双向 table() 转换为 R 中的成对计数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文