Exercise3

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
data(nlschools,package="MASS")
knitr::kable(head(nlschools))
lang IQ class GS SES COMB
46 15.0 180 29 23 0
45 14.5 180 29 10 0
33 9.5 180 29 15 0
46 11.0 180 29 23 0
20 8.0 180 29 10 0
30 9.5 180 29 10 0
dta <- as_tibble(MASS::nlschools)
dta1 <- dta %>% group_by(class) %>% summarize(total=n()) %>% 
  filter(total >30)
dta2 <- dta[dta$class %in% dta1$class, ]

dta2$class <- dta2$class %>% as.character() 
dta2 <-split(dta2,dta2$class)
plot.new()
par(mfrow=c(2, 3), mar=c(2, 2, 2, 1))
lapply(dta2, function(x) {hist(x$IQ,main = "histogram of IQ",
                                               axes = F,
                                               breaks = seq(6, 18, by =
                                                              2))
                                           axis(1,
                                                   at = seq(6, 18, 2))
                                               legend("topright",
                                                 paste("class",
                                                       x$class[1],
                                                       sep = ": "),
                                                 bty="n")})
## $`15580`
## $`15580`$rect
## $`15580`$rect$w
## [1] 5.009586
## 
## $`15580`$rect$h
## [1] 2.024179
## 
## $`15580`$rect$left
## [1] 13.47041
## 
## $`15580`$rect$top
## [1] 14.56
## 
## 
## $`15580`$text
## $`15580`$text$x
## [1] 14.60051
## 
## $`15580`$text$y
## [1] 13.54791
## 
## 
## 
## $`15980`
## $`15980`$rect
## $`15980`$rect$w
## [1] 5.009586
## 
## $`15980`$rect$h
## [1] 2.024179
## 
## $`15980`$rect$left
## [1] 13.47041
## 
## $`15980`$rect$top
## [1] 14.56
## 
## 
## $`15980`$text
## $`15980`$text$x
## [1] 14.60051
## 
## $`15980`$text$y
## [1] 13.54791
## 
## 
## 
## $`16180`
## $`16180`$rect
## $`16180`$rect$w
## [1] 5.009586
## 
## $`16180`$rect$h
## [1] 2.168763
## 
## $`16180`$rect$left
## [1] 13.47041
## 
## $`16180`$rect$top
## [1] 15.6
## 
## 
## $`16180`$text
## $`16180`$text$x
## [1] 14.60051
## 
## $`16180`$text$y
## [1] 14.51562
## 
## 
## 
## $`18380`
## $`18380`$rect
## $`18380`$rect$w
## [1] 5.009586
## 
## $`18380`$rect$h
## [1] 2.891684
## 
## $`18380`$rect$left
## [1] 13.47041
## 
## $`18380`$rect$top
## [1] 20.8
## 
## 
## $`18380`$text
## $`18380`$text$x
## [1] 14.60051
## 
## $`18380`$text$y
## [1] 19.35416
## 
## 
## 
## $`5480`
## $`5480`$rect
## $`5480`$rect$w
## [1] 4.652863
## 
## $`5480`$rect$h
## [1] 1.879595
## 
## $`5480`$rect$left
## [1] 13.82714
## 
## $`5480`$rect$top
## [1] 13.52
## 
## 
## $`5480`$text
## $`5480`$text$x
## [1] 14.95724
## 
## $`5480`$text$y
## [1] 12.5802