7 Grouping
#summarize() + group_by() to create grouped summaries
summarize(flights,delay = mean(dep_delay, na.rm=TRUE)) #calculates average departure delay
#quartiles and means
summary(swiss) #gives quartiles & mean for all variables
summary(swiss$Fertility) #quartiles and means for that variable
mean(swiss\(Fertility) #mean var(swiss\)Fertility) #variance
#to summarize subsets summary(swiss\(Fertility[swiss\)Agriculture>50]) #summarize fertility where agriculture is greater than 50 summary(swiss\(Fertility[swiss\)Agriculture>50 | swiss$Catholic>50]) #where agr>50 OR Catholic>50 #“|” for “or” #“&” for “and” #“==” for “equals”
#I think I found a way to do this using the tidyverse approach, but it's clunkier than the summary() function
bigag <- filter(swiss,Agriculture>50)
summarize(bigag,avgfert=mean(Fertility), medfert=median(Fertility))