user7452755
user7452755

Reputation:

R - SQL aggregate min and max

Currently, I want to output dataframe from iris data set that will cover following query:

SELECT MIN(Sepal.Width), MAX(Sepal.Width), Sepal.Length, species FROM iris GROUP BY Sepal.Length, species

I create that code:

query<-as.data.frame(aggregate(. ~ Sepal.Length, species, data = iris, FUN = function(x) c(min = min(x), maxi = max(x) ) ))

But it does not output my expected result. I did that in sqldf and dplyr, but my question is:

How to do this with only R-Base functions?

Upvotes: 0

Views: 746

Answers (1)

Pdubbs
Pdubbs

Reputation: 1987

You're very close. Your problem is that aggregate uses the formula interface instead of by, not the two in conjunction. If you want to mimic GROUP BY from sql with multiple groups, put them on the right side of your formula.

query<-as.data.frame(aggregate(. ~ Species+Sepal.Length, 
                           data = iris, 
                           FUN = function(x) c(min = min(x), maxi = max(x) ) ))

If you're only interested in one trait, substitute it for the . on the left side of your formula.

query<-as.data.frame(aggregate(Sepal.Width ~ Species+Sepal.Length, 
                           data = iris, 
                           FUN = function(x) c(min = min(x), maxi = max(x) ) ))

Upvotes: 2

Related Questions