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

2011-01-26 Thread Ryan Utz
Hadley and Dennis: THANK YOU THANK YOU! This is exactly what I was looking for. Ryan On Wed, Jan 26, 2011 at 5:27 AM, Dennis Murphy wrote: > > Hi: > > > > Here are two more candidates, using the plyr and data.table packages: > > > > library(plyr) > > ddply(X, .(x, y), function(d) length(unique

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

2011-01-26 Thread Hadley Wickham
On Wed, Jan 26, 2011 at 5:27 AM, Dennis Murphy 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

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

2011-01-26 Thread Matthew Dowle
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=l

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

2011-01-25 Thread Dennis Murphy
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

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

2011-01-25 Thread David Winsemius
On Jan 25, 2011, at 2:25 PM, Ryan Utz wrote: Hi R-users, I'm trying to find an elegant way to count the number of rows in a dataframe with a unique combination of 2 values in the dataframe. My data is specifically one column with a year, one with a month, and one with a day. I'm trying to

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

2011-01-25 Thread Ista Zahn
Hi Ryan, One option would be X$a <- paste(X$x, X$y, sep=".") table(X$a) Best, Ista On Tue, Jan 25, 2011 at 2:25 PM, Ryan Utz wrote: > Hi R-users, > > I'm trying to find an elegant way to count the number of rows in a dataframe > with a unique combination of 2 values in the dataframe. My data is

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

2011-01-25 Thread Henrique Dallazuanna
If you want count: xtabs( ~ x + y, X) or sum: xtabs(z ~ x + y, X) On Tue, Jan 25, 2011 at 5:25 PM, Ryan Utz wrote: > Hi R-users, > > I'm trying to find an elegant way to count the number of rows in a > dataframe > with a unique combination of 2 values in the dataframe. My data is > specifi

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

2011-01-25 Thread Ryan Utz
Hi R-users, I'm trying to find an elegant way to count the number of rows in a dataframe with a unique combination of 2 values in the dataframe. My data is specifically one column with a year, one with a month, and one with a day. I'm trying to count the number of days in each year/month combinati