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 <table> 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-R-tp23020587p23020587.html Sent from the R help mailing list archive at Nabble.com.
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 <table> GROUP BY f1, f2, ..,f4. > > How to do this in R?table(f1,f2,f3,f4) will give you the counts. Other statistics can by computed by group using the by() function. Duncan Murdoch
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 <nikolay12 at yahoo.com> 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 <table> 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-R-tp23020587p23020587.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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. >
One way: g= paste(f1,f2,f3,f4) table(g) On Mon, Apr 13, 2009 at 7:33 AM, Nick Angelou <nikolay12 at yahoo.com> 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 <table> 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-R-tp23020587p23020587.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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. >-- Mike Lawrence Graduate Student Department of Psychology Dalhousie University Looking to arrange a meeting? Check my public calendar: http://tinyurl.com/mikes-public-calendar ~ Certainty is folly... I think. ~
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 combinations of factors f1, f2, f3, as columns the unique values of f4. The counts are basically the same as of the GROUP BY statement (or the paste and table combination suggested by Mike). Only the way the result is structured is different. Thanks, Nick 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 <table> 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-R-tp23020587p23020963.html Sent from the R help mailing list archive at Nabble.com.
Seemingly Similar Threads
- sem problem - did not converge
- [LLVMdev] [NVPTX] llc -march=nvptx64 -mcpu=sm_20 generates invalid zero align for device function params
- Find max of a row in data frame (like Excel)
- How should I denormalise a data frame list of lists column?
- How to filter information from a big .csv table into a new table