[R] Counter

Jorge Ivan Velez
Thu, 23 Jul 2009 13:36:51 -0000

Try also:
t( apply(x, 1, function(x) table( factor(x, levels = 0:2) ) ) )
#       0 1 2
# [1,] 1 2 1
# [2,] 1 3 0
# [3,] 3 0 1
# [4,] 0 2 2

See ?apply, ?table and ?factor for more details, examples and information.

HTH,

Jorge


On Thu, Jul 23, 2009 at 6:44 AM, amor Gandhi <amorigandhi@yahoo.de> wrote:

> Hi everyone,
>
> Is there any counter function in R for the following purpose:
> x <- matrix(c(1,1,0,2,1,0,0,2,0,1,2,1,2,1,0,1),nrow=4)
> As I would like to know how many zeros, ones, and twos in each row of x?
>
> Many thank in advance,
> Amor
>
>
>
>
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help@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.
>
>

	[[alternative HTML version deleted]]