[R] output from table() in matrix form

Robin Hankin r.hankin at soc.soton.ac.uk
Wed Jan 5 14:19:13 CET 2005


On Jan 5, 2005, at 12:59 pm, Prof Brian Ripley wrote:

> table works the way it does because it applies to *factors*, so the 
> names are the factor levels of the argument after conversion.  So if 
> anything is wasteful, that is.
>
> How about using the guts of factor and table, via
>
> xx <- unique(x)
> rbind(vals=xx, cnts=tabulate(match(x, xx)))
>
> ?
>



yes!  this is just what I needed.   For me, it's good for another 
reason too: this
method does not suffer if x contains a single enormous value (common
in my application).

I found this very instructive.  Could we add this concatenation
of tabulate() with match() to one or both manpages?





> On Wed, 5 Jan 2005, Robin Hankin wrote:
>
>> Hi
>>
>> How do I get the output from table() in matrix form?
>>
>> If I have
>>
>> R>  table(c(1,1,1,1,2,20))
>>
>> 1  2 20
>> 4  1  1
>>
>> I want
>>
>>     [,1] [,2] [,3]
>> [1,]    1    2   20
>> [2,]    4    1    1
--
Robin Hankin
Uncertainty Analyst
Southampton Oceanography Centre
European Way, Southampton SO14 3ZH, UK
  tel  023-8059-7743




More information about the R-help mailing list