Re: [R] Group by in R

2009-04-14 Thread Paul Johnson
On Mon, Apr 13, 2009 at 8:56 AM, Nick Angelou wrote: > >> data >   X1 X2 X3 X4 > 1   1  2  2  1 > 2   1  1  2  2 > 3   1  1  2  2 > 4   2  2  1  2 > 5   1  1  2  2 > 6   2  2  1  2 > 7   1  1  2  1 > 8   2  2  1  2 > 9   1  2  1  1 > 10  1  1  2  2 > > sqldf("select X1, X2, X3, X4, count(*) CNT fr

Re: [R] Group by in R

2009-04-13 Thread Gabor Grothendieck
Assuming DF is your data frame try this: ftable(DF) In SQL you can get close with: sqldf("select X1, X2, X3, sum(X4 == 1) `X4=1`, sum(X4 == 2) `X4=2` from DF group by X1, X2, X3 order by X1, X2, X3") On Mon, Apr 13, 2009 at 9:56 AM, Nick Angelou wrote: > > > Gabor Grothendieck wrote: >> >> SQL

Re: [R] Group by in R

2009-04-13 Thread Nick Angelou
Gabor Grothendieck wrote: > > SQL has the order by clause. > Gabor, thanks for the suggestion. I thought about this but ORDER BY cannot create the tabular structure that I need. Here is more detail about my setting: f1, f2, f3 have unique triplets (each repeating a different number of times).

Re: [R] Group by in R

2009-04-13 Thread David Winsemius
On Apr 13, 2009, at 7:26 AM, Nick Angelou wrote: Thanks a lot, guys. Gabor's and Mike's suggestion worked. Duncan's did not do exactly what I expected (I guess it's the "paste" in Mike's that makes "table" work as I needed it). One more question - is there a convenient way to order the gro

Re: [R] Group by in R

2009-04-13 Thread Gabor Grothendieck
SQL has the order by clause. On Mon, Apr 13, 2009 at 7:26 AM, Nick Angelou wrote: > > Thanks a lot, guys. Gabor's and Mike's suggestion worked. Duncan's did not do > exactly what I expected (I guess it's the "paste" in Mike's that makes > "table" work as I needed it). > > One more question - is t

Re: [R] Group by in R

2009-04-13 Thread Nick Angelou
Thanks a lot, guys. Gabor's and Mike's suggestion worked. Duncan's did not do exactly what I expected (I guess it's the "paste" in Mike's that makes "table" work as I needed it). One more question - is there a convenient way to order the group by results as follows: As rows: the unique combinati

Re: [R] Group by in R

2009-04-13 Thread Peter Dalgaard
Mike Lawrence wrote: One way: g= paste(f1,f2,f3,f4) table(g) I'd go for g <- interaction(f1,f2,f3,f4, drop=TRUE) table(g) which is essentially the same thing. On Mon, Apr 13, 2009 at 7:33 AM, Nick Angelou wrote: Hi, I have the following table data: f1, f2, f3, f4. I want to compute t

Re: [R] Group by in R

2009-04-13 Thread Mike Lawrence
One way: g= paste(f1,f2,f3,f4) table(g) On Mon, Apr 13, 2009 at 7:33 AM, Nick Angelou wrote: > > Hi, > > I have the following table data: > > f1, f2, f3, f4. > > I want to compute the counts of unique combinations of f1-f4. In SQL I would > just write: > > SELECT COUNT(*) FROM GROUP BY f1, f2,

Re: [R] Group by in R

2009-04-13 Thread Gabor Grothendieck
You can use SQL commands directly on R data frames with the R sqldf package: See home page: http://sqldf.googlecode.com On Mon, Apr 13, 2009 at 6:33 AM, Nick Angelou wrote: > > Hi, > > I have the following table data: > > f1, f2, f3, f4. > > I want to compute the counts of unique combinations of

Re: [R] Group by in R

2009-04-13 Thread Duncan Murdoch
Nick Angelou wrote: Hi, I have the following table data: f1, f2, f3, f4. I want to compute the counts of unique combinations of f1-f4. In SQL I would just write: SELECT COUNT(*) FROM GROUP BY f1, f2, ..,f4. How to do this in R? table(f1,f2,f3,f4) will give you the counts. Other statistic

[R] Group by in R

2009-04-13 Thread Nick Angelou
Hi, I have the following table data: f1, f2, f3, f4. I want to compute the counts of unique combinations of f1-f4. In SQL I would just write: SELECT COUNT(*) FROM GROUP BY f1, f2, ..,f4. How to do this in R? Thanks, Nick -- View this message in context: http://www.nabble.com/Group-by-in-