Aurore Vaitinadapoule
Aurore Vaitinadapoule

Reputation: 153

Contingency table once against multiple table

I have a table like the following:

V1  V2  V3  V4  V5  V6  V7  V8  V9 V10
fve fve fve fve fve fve fve fve mdm mdm
mdm fve fve fve fve fve fve fve fve fve
fve fve fve fve fve fve fve fve fve fve
mdm fve fve fve fve fve fve fve fve fve
fve fve fve fve fve fve fve fve fve fve

and I would like to count the frequence from column 1 against all and get something like this:

    fve mdm
fve 25  2
mdm 18  0

Is it possible to do it ? I tried with table() and ftable() but output is not corresponding to what I'm expecting.

Upvotes: 2

Views: 83

Answers (2)

akrun
akrun

Reputation: 887941

Slightly different option is

table(df1[,1][row(df1[-1])], unlist(df1[-1])) 
#     fve mdm
# fve  25   2
# mdm  18   0

Upvotes: 2

IRTFM
IRTFM

Reputation: 263481

For the modified question:

> tapply(unlist(dat[-1]), rep(dat[[1]], length(dat[-1])), table)
$fve

fve mdm 
 25   2 

$mdm

fve mdm 
 18   0 

I suppose you could run do.call(rbind, ...) on those values to get the desired table:

Upvotes: 3

Related Questions