Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting unique pairs of categorical variables in R [duplicate]

Tags:

r

frequency

If I have this data:

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

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?

like image 203
theamateurdataanalyst Avatar asked Jul 24 '14 04:07

theamateurdataanalyst


2 Answers

table(as.character(interaction(df)))

Or

library(qdap)
table(paste2(df))

# X.A X.B Y.B Z.B Z.C 
#   2   2   3   1   1 
like image 150
akrun Avatar answered Oct 24 '22 04:10

akrun


This is the dplyr solution.

library(dplyr)

df %>% group_by(One,Two) %>%
      summarize(Count = n())

This returns a data frame like this

Source: local data frame [5 x 3]
Groups: One

  One Two Count
1   X   A     2
2   X   B     2
3   Y   B     3
4   Z   B     1
5   Z   C     1
like image 28
Koundy Avatar answered Oct 24 '22 04:10

Koundy