[R] what is the "right" way to obtain frequencies of vector values?

jim holtman jholtman at gmail.com
Thu Nov 15 11:41:32 CET 2007


You can also try 'tabulate' if you want a vector of size N:

> tabulate(c(1,1,3,4,12,123,12,2,21,2,2,2))
  [1] 2 4 1 1 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 [51] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[101] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
>


On Nov 15, 2007 5:09 AM, Dieter Menne <dieter.menne at menne-biomed.de> wrote:
> Vlad Skvortsov <vss <at> 73rus.com> writes:
> > Let's say I have vector x with positive integer values ranging from 1 to
> > N. I need to obtain another vector y of size N where y[i] contains the
> > number of times value i occurs in x. It is in a sense similar to hist()
> > (with appropriate number of breaks) or table() with numeric "factors".
> >
> > Currenlty I use a custom function for that, but thought maybe there is a
> > more "direct" way in R.
>
> table works with numerics:
>
> table(c(1,1,3,4,12,123,12,2,21,2,2,2))
>
>  1   2   3   4  12  21 123
>  2   4   1   1   2   1   1
>
> Dieter
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list