Skip to content
Prev 248404 / 398502 Next

Counting number of rows with two criteria in dataframe

Note that a key is not actually required, so it's even simpler syntax :

dX = as.data.table(X)
dX[,length(unique(z)),by="x,y"]
     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

or passing list() syntax to the 'by' is exactly the same :

dX[,length(unique(z)),by=list(x,y)]

The advantage of using the list() form is you can group by expressions
of columns, for example if x was a date column :

dX[,length(unique(z)),by=list(month(x),y)]

Matthew


"Dennis Murphy" <djmuser at gmail.com> wrote in message 
news:AANLkTi=8TYSrRfzfm01m7fpzydh-cLS-J-cMbkAkjXxf at mail.gmail.com...