[R] Counting number of rows with two criteria in dataframe

Hadley Wickham hadley at rice.edu
Wed Jan 26 14:40:38 CET 2011


On Wed, Jan 26, 2011 at 5:27 AM, Dennis Murphy <djmuser at gmail.com> wrote:
> Hi:
>
> Here are two more candidates, using the plyr and data.table packages:
>
> library(plyr)
> ddply(X, .(x, y), function(d) length(unique(d$z)))
>  x y V1
> 1 1 1  2
> 2 1 2  2
> 3 2 3  2
> 4 2 4  2
> 5 3 5  2
> 6 3 6  2
>
> The function counts the number of unique z values in each sub-data frame
> with the same x and y values. The argument d in the anonymous function is a
> data frame object.

Another approach is to use the much faster count function:

count(unique(X))

Hadley

-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/



More information about the R-help mailing list