summary()
関数で、データ全体の集計が行える。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
> name.data <- c("John", "Gene", "Jack", "Luice", "Elen") > gender.data <- c("M", "F", "M", "M", "F") > height.data <- c(165, 160, 170, 178, 157) > weight.data <- c(62, 56, 75, 82, 58) > qualified.data <- c(T, F, T, F, T) > > data <- data.frame( + name=name.data, + gender=gender.data, + height=height.data, + weight=weight.data, + qualified=qualified.data + ) > data$name <- as.character(data$name) > > summary(data) name gender height weight qualified Length:5 F:2 Min. :157 Min. :56.0 Mode :logical Class :character M:3 1st Qu.:160 1st Qu.:58.0 FALSE:2 Mode :character Median :165 Median :62.0 TRUE :3 Mean :166 Mean :66.6 NA's :0 3rd Qu.:170 3rd Qu.:75.0 Max. :178 Max. :82.0 |
by()
関数によって、特定のデータの層別に集計することができる。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
> by(data, data$gender, summary) data$gender: F name gender height weight qualified Length:2 F:2 Min. :157.0 Min. :56.0 Mode :logical Class :character M:0 1st Qu.:157.8 1st Qu.:56.5 FALSE:1 Mode :character Median :158.5 Median :57.0 TRUE :1 Mean :158.5 Mean :57.0 NA's :0 3rd Qu.:159.2 3rd Qu.:57.5 Max. :160.0 Max. :58.0 --------------------------------------------------------- data$gender: M name gender height weight qualified Length:3 F:0 Min. :165.0 Min. :62.0 Mode :logical Class :character M:3 1st Qu.:167.5 1st Qu.:68.5 FALSE:1 Mode :character Median :170.0 Median :75.0 TRUE :2 Mean :171.0 Mean :73.0 NA's :0 3rd Qu.:174.0 3rd Qu.:78.5 Max. :178.0 Max. :82.0 |
もちろん、個別の数値データについて各種代表値を計算することもできる。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
> max(data$height) [1] 178 > min(data$height) [1] 157 > range(data$height) [1] 157 178 > sum(data$height) [1] 830 > mean(data$height) [1] 166 > var(data$height) [1] 69.5 > sd(data$height) [1] 8.336666 |