transformation - Transforming a two-way table() into pairwise list of counts in R -


starting 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   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 transform table following format:

goal <- data.frame(a=c(1,2,3,4),b=c(1,2,3,4),count=c(1,3,1,2))     > goal   b count 1 1 1     1 2 2 2     3 3 3 3     1 4 4 4     2 . . .     . 

how can form pairwise combinations two-way table , add frequency counts in third column?

intuition tells me there should simple kind of 'reverse' function table, not find on or google.

naturally, after posting question found right search query google...

> as.data.frame(tab)    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 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -