2

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

2 Answers2

3

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:

IRTFM
  • 258,963
  • 21
  • 364
  • 487
2

Slightly different option is

table(df1[,1][row(df1[-1])], unlist(df1[-1])) 
#     fve mdm
# fve  25   2
# mdm  18   0
akrun
  • 874,273
  • 37
  • 540
  • 662