R - SQL aggregate min and max -


currently, want output dataframe iris data set cover following query:

select min(sepal.width), max(sepal.width), sepal.length, species iris group sepal.length, species

i create code:

query<-as.data.frame(aggregate(. ~ sepal.length, species, data = iris, fun = function(x) c(min = min(x), maxi = max(x) ) ))

but not output expected result. did in sqldf , dplyr, question is:

how r-base functions?

you're close. problem aggregate uses formula interface instead of by, not 2 in conjunction. if want mimic group by sql multiple groups, put them on right side of formula.

query<-as.data.frame(aggregate(. ~ species+sepal.length,                             data = iris,                             fun = function(x) c(min = min(x), maxi = max(x) ) )) 

if you're interested in 1 trait, substitute . on left side of formula.

query<-as.data.frame(aggregate(sepal.width ~ species+sepal.length,                             data = iris,                             fun = function(x) c(min = min(x), maxi = max(x) ) )) 

Comments