计算R中类别变量的唯一对 [英] Counting unique pairs of categorical variables in R
本文介绍了计算R中类别变量的唯一对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有此数据:
One <- c(rep("X",4),rep("Y",3),rep("Z",2))
Two <- c(rep("A",2),rep("B",6),rep("C",1))
df <- data.frame(One,Two)
One Two
1 X A
2 X A
3 X B
4 X B
5 Y B
6 Y B
7 Y B
8 Z B
9 Z C
我想找到唯一对(一对,一对)的频率.我知道如果我想在列One
中查找不同元素的频率,我会执行table(df$one)
.唯一对的频率如何?
I want to find the frequency of unique pairs (one,two). I know if I wanted to find the frequency of different elements in column One
I would do table(df$one)
. How about the frequency of unique pairs?
推荐答案
table(as.character(interaction(df)))
或
library(qdap)
table(paste2(df))
# X.A X.B Y.B Z.B Z.C
# 2 2 3 1 1
这篇关于计算R中类别变量的唯一对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文