ls()
## character(0)
rm(list=ls())
getwd()
## [1] "C:/R"
setwd("c:/R")
read.csv("mpg1.csv")
## manufacturer trans drv cty hwy
## 1 audi auto f 18 29
## 2 audi manual f 21 29
## 3 audi manual f 20 31
## 4 audi auto f 21 30
## 5 audi auto f 16 26
## 6 audi manual f 18 26
## 7 audi auto f 18 27
## 8 audi manual 4 18 26
## 9 audi auto 4 16 25
## 10 audi manual 4 20 28
## 11 audi auto 4 19 27
## 12 audi auto 4 15 25
## 13 audi manual 4 17 25
## 14 audi auto 4 17 25
## 15 audi manual 4 15 25
## 16 audi auto 4 15 24
## 17 audi auto 4 17 25
## 18 audi auto 4 16 23
## 19 chevrolet auto r 14 20
## 20 chevrolet auto r 11 15
## 21 chevrolet auto r 14 20
## 22 chevrolet auto r 13 17
## 23 chevrolet auto r 12 17
## 24 chevrolet manual r 16 26
## 25 chevrolet auto r 15 23
## 26 chevrolet manual r 16 26
## 27 chevrolet auto r 15 25
## 28 chevrolet manual r 15 24
## 29 chevrolet auto 4 14 19
## 30 chevrolet auto 4 11 14
## 31 chevrolet auto 4 11 15
## 32 chevrolet auto 4 14 17
## 33 chevrolet auto f 19 27
## 34 chevrolet auto f 22 30
## 35 chevrolet auto f 18 26
## 36 chevrolet auto f 18 29
## 37 chevrolet auto f 17 26
## 38 dodge auto f 18 24
## 39 dodge auto f 17 24
## 40 dodge auto f 16 22
## 41 dodge auto f 16 22
## 42 dodge auto f 17 24
## 43 dodge auto f 17 24
## 44 dodge auto f 11 17
## 45 dodge auto f 15 22
## 46 dodge auto f 15 21
## 47 dodge auto f 16 23
## 48 dodge auto f 16 23
## 49 dodge manual 4 15 19
## 50 dodge auto 4 14 18
## 51 dodge auto 4 13 17
## 52 dodge manual 4 14 17
## 53 dodge auto 4 14 19
## 54 dodge auto 4 14 19
## 55 dodge auto 4 9 12
## 56 dodge manual 4 11 17
## 57 dodge auto 4 11 15
## 58 dodge auto 4 13 17
## 59 dodge auto 4 13 17
## 60 dodge auto 4 9 12
## 61 dodge auto 4 13 17
## 62 dodge auto 4 11 16
## 63 dodge auto 4 13 18
## 64 dodge auto 4 11 15
## 65 dodge manual 4 12 16
## 66 dodge auto 4 9 12
## 67 dodge auto 4 13 17
## 68 dodge auto 4 13 17
## 69 dodge manual 4 12 16
## 70 dodge manual 4 9 12
## 71 dodge auto 4 11 15
## 72 dodge manual 4 11 16
## 73 dodge auto 4 13 17
## 74 dodge auto 4 11 15
## 75 ford auto r 11 17
## 76 ford auto r 11 17
## 77 ford auto r 12 18
## 78 ford auto 4 14 17
## 79 ford manual 4 15 19
## 80 ford auto 4 14 17
## 81 ford auto 4 13 19
## 82 ford auto 4 13 19
## 83 ford auto 4 13 17
## 84 ford auto 4 14 17
## 85 ford manual 4 14 17
## 86 ford manual 4 13 16
## 87 ford auto 4 13 16
## 88 ford auto 4 13 17
## 89 ford auto 4 11 15
## 90 ford auto 4 13 17
## 91 ford manual r 18 26
## 92 ford auto r 18 25
## 93 ford manual r 17 26
## 94 ford auto r 16 24
## 95 ford auto r 15 21
## 96 ford manual r 15 22
## 97 ford manual r 15 23
## 98 ford auto r 15 22
## 99 ford manual r 14 20
## 100 honda manual f 28 33
## 101 honda auto f 24 32
## 102 honda manual f 25 32
## 103 honda manual f 23 29
## 104 honda auto f 24 32
## 105 honda manual f 26 34
## 106 honda auto f 25 36
## 107 honda auto f 24 36
## 108 honda manual f 21 29
## 109 hyundai auto f 18 26
## 110 hyundai manual f 18 27
## 111 hyundai auto f 21 30
## 112 hyundai manual f 21 31
## 113 hyundai auto f 18 26
## 114 hyundai manual f 18 26
## 115 hyundai auto f 19 28
## 116 hyundai auto f 19 26
## 117 hyundai manual f 19 29
## 118 hyundai manual f 20 28
## 119 hyundai auto f 20 27
## 120 hyundai auto f 17 24
## 121 hyundai manual f 16 24
## 122 hyundai manual f 17 24
## 123 jeep auto 4 17 22
## 124 jeep auto 4 15 19
## 125 jeep auto 4 15 20
## 126 jeep auto 4 14 17
## 127 jeep auto 4 9 12
## 128 jeep auto 4 14 19
## 129 jeep auto 4 13 18
## 130 jeep auto 4 11 14
## 131 land rover auto 4 11 15
## 132 land rover auto 4 12 18
## 133 land rover auto 4 12 18
## 134 land rover auto 4 11 15
## 135 lincoln auto r 11 17
## 136 lincoln auto r 11 16
## 137 lincoln auto r 12 18
## 138 mercury auto 4 14 17
## 139 mercury auto 4 13 19
## 140 mercury auto 4 13 19
## 141 mercury auto 4 13 17
## 142 nissan manual f 21 29
## 143 nissan auto f 19 27
## 144 nissan auto f 23 31
## 145 nissan manual f 23 32
## 146 nissan manual f 19 27
## 147 nissan auto f 19 26
## 148 nissan auto f 18 26
## 149 nissan manual f 19 25
## 150 nissan auto f 19 25
## 151 nissan auto 4 14 17
## 152 nissan manual 4 15 17
## 153 nissan auto 4 14 20
## 154 nissan auto 4 12 18
## 155 pontiac auto f 18 26
## 156 pontiac auto f 16 26
## 157 pontiac auto f 17 27
## 158 pontiac auto f 18 28
## 159 pontiac auto f 16 25
## 160 subaru manual 4 18 25
## 161 subaru auto 4 18 24
## 162 subaru manual 4 20 27
## 163 subaru manual 4 19 25
## 164 subaru auto 4 20 26
## 165 subaru auto 4 18 23
## 166 subaru auto 4 21 26
## 167 subaru manual 4 19 26
## 168 subaru manual 4 19 26
## 169 subaru auto 4 19 26
## 170 subaru auto 4 20 25
## 171 subaru auto 4 20 27
## 172 subaru manual 4 19 25
## 173 subaru manual 4 20 27
## 174 toyota manual 4 15 20
## 175 toyota auto 4 16 20
## 176 toyota auto 4 15 19
## 177 toyota manual 4 15 17
## 178 toyota auto 4 16 20
## 179 toyota auto 4 14 17
## 180 toyota manual f 21 29
## 181 toyota auto f 21 27
## 182 toyota manual f 21 31
## 183 toyota auto f 21 31
## 184 toyota auto f 18 26
## 185 toyota manual f 18 26
## 186 toyota auto f 19 28
## 187 toyota auto f 21 27
## 188 toyota manual f 21 29
## 189 toyota manual f 21 31
## 190 toyota auto f 22 31
## 191 toyota auto f 18 26
## 192 toyota manual f 18 26
## 193 toyota auto f 18 27
## 194 toyota auto f 24 30
## 195 toyota auto f 24 33
## 196 toyota manual f 26 35
## 197 toyota manual f 28 37
## 198 toyota auto f 26 35
## 199 toyota auto 4 11 15
## 200 toyota auto 4 13 18
## 201 toyota manual 4 15 20
## 202 toyota auto 4 16 20
## 203 toyota manual 4 17 22
## 204 toyota manual 4 15 17
## 205 toyota auto 4 15 19
## 206 toyota manual 4 15 18
## 207 toyota auto 4 16 20
## 208 volkswagen manual f 21 29
## 209 volkswagen auto f 19 26
## 210 volkswagen manual f 21 29
## 211 volkswagen auto f 22 29
## 212 volkswagen manual f 17 24
## 213 volkswagen manual f 33 44
## 214 volkswagen manual f 21 29
## 215 volkswagen auto f 19 26
## 216 volkswagen auto f 22 29
## 217 volkswagen manual f 21 29
## 218 volkswagen auto f 21 29
## 219 volkswagen manual f 21 29
## 220 volkswagen auto f 16 23
## 221 volkswagen manual f 17 24
## 222 volkswagen manual f 35 44
## 223 volkswagen auto f 29 41
## 224 volkswagen manual f 21 29
## 225 volkswagen auto f 19 26
## 226 volkswagen manual f 20 28
## 227 volkswagen auto f 20 29
## 228 volkswagen manual f 21 29
## 229 volkswagen auto f 18 29
## 230 volkswagen auto f 19 28
## 231 volkswagen manual f 21 29
## 232 volkswagen auto f 16 26
## 233 volkswagen manual f 18 26
## 234 volkswagen auto f 17 26
mpg1<-read.csv("c:/R/mpg1.csv")
str(mpg1)
## 'data.frame': 234 obs. of 5 variables:
## $ manufacturer: chr "audi" "audi" "audi" "audi" ...
## $ trans : chr "auto" "manual" "manual" "auto" ...
## $ drv : chr "f" "f" "f" "f" ...
## $ cty : int 18 21 20 21 16 18 18 18 16 20 ...
## $ hwy : int 29 29 31 30 26 26 27 26 25 28 ...
table(mpg1$trans)
##
## auto manual
## 157 77
table(mpg1$trans,mpg1$manufacturer)
##
## audi chevrolet dodge ford honda hyundai jeep land rover lincoln
## auto 11 16 30 17 4 7 8 4 3
## manual 7 3 7 8 5 7 0 0 0
##
## mercury nissan pontiac subaru toyota volkswagen
## auto 4 8 5 7 20 13
## manual 0 5 0 7 14 14
table(mpg1$trans,mpg1$drv)
##
## 4 f r
## auto 75 65 17
## manual 28 41 8
a<-table(mpg1$trans)
prop.table(a)
##
## auto manual
## 0.6709402 0.3290598
round(prop.table(a),2)
##
## auto manual
## 0.67 0.33
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.6 v dplyr 1.0.8
## v tidyr 1.2.0 v stringr 1.4.0
## v readr 2.1.2 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(dplyr)
#ctrl+shift+m
mpg1_1<-mpg1 %>%rename(t=trans,m=manufacturer)
head(mpg1_1)
## m t drv cty hwy
## 1 audi auto f 18 29
## 2 audi manual f 21 29
## 3 audi manual f 20 31
## 4 audi auto f 21 30
## 5 audi auto f 16 26
## 6 audi manual f 18 26
mpg1_2<-mpg1_1 %>% select(t,drv,cty)
head(mpg1_2)
## t drv cty
## 1 auto f 18
## 2 manual f 21
## 3 manual f 20
## 4 auto f 21
## 5 auto f 16
## 6 manual f 18
mpg1_3<-mpg1 %>% filter(manufacturer=="audi"&trans=="auto"&drv=="f")
head(mpg1_3)
## manufacturer trans drv cty hwy
## 1 audi auto f 18 29
## 2 audi auto f 21 30
## 3 audi auto f 16 26
## 4 audi auto f 18 27
# bigdata_class end(04_09)
max(mpg1$cty)
## [1] 35
min(mpg1$cty)
## [1] 9
range(mpg1$cty)
## [1] 9 35
mpg1 %>% filter(cty<16)
## manufacturer trans drv cty hwy
## 1 audi auto 4 15 25
## 2 audi manual 4 15 25
## 3 audi auto 4 15 24
## 4 chevrolet auto r 14 20
## 5 chevrolet auto r 11 15
## 6 chevrolet auto r 14 20
## 7 chevrolet auto r 13 17
## 8 chevrolet auto r 12 17
## 9 chevrolet auto r 15 23
## 10 chevrolet auto r 15 25
## 11 chevrolet manual r 15 24
## 12 chevrolet auto 4 14 19
## 13 chevrolet auto 4 11 14
## 14 chevrolet auto 4 11 15
## 15 chevrolet auto 4 14 17
## 16 dodge auto f 11 17
## 17 dodge auto f 15 22
## 18 dodge auto f 15 21
## 19 dodge manual 4 15 19
## 20 dodge auto 4 14 18
## 21 dodge auto 4 13 17
## 22 dodge manual 4 14 17
## 23 dodge auto 4 14 19
## 24 dodge auto 4 14 19
## 25 dodge auto 4 9 12
## 26 dodge manual 4 11 17
## 27 dodge auto 4 11 15
## 28 dodge auto 4 13 17
## 29 dodge auto 4 13 17
## 30 dodge auto 4 9 12
## 31 dodge auto 4 13 17
## 32 dodge auto 4 11 16
## 33 dodge auto 4 13 18
## 34 dodge auto 4 11 15
## 35 dodge manual 4 12 16
## 36 dodge auto 4 9 12
## 37 dodge auto 4 13 17
## 38 dodge auto 4 13 17
## 39 dodge manual 4 12 16
## 40 dodge manual 4 9 12
## 41 dodge auto 4 11 15
## 42 dodge manual 4 11 16
## 43 dodge auto 4 13 17
## 44 dodge auto 4 11 15
## 45 ford auto r 11 17
## 46 ford auto r 11 17
## 47 ford auto r 12 18
## 48 ford auto 4 14 17
## 49 ford manual 4 15 19
## 50 ford auto 4 14 17
## 51 ford auto 4 13 19
## 52 ford auto 4 13 19
## 53 ford auto 4 13 17
## 54 ford auto 4 14 17
## 55 ford manual 4 14 17
## 56 ford manual 4 13 16
## 57 ford auto 4 13 16
## 58 ford auto 4 13 17
## 59 ford auto 4 11 15
## 60 ford auto 4 13 17
## 61 ford auto r 15 21
## 62 ford manual r 15 22
## 63 ford manual r 15 23
## 64 ford auto r 15 22
## 65 ford manual r 14 20
## 66 jeep auto 4 15 19
## 67 jeep auto 4 15 20
## 68 jeep auto 4 14 17
## 69 jeep auto 4 9 12
## 70 jeep auto 4 14 19
## 71 jeep auto 4 13 18
## 72 jeep auto 4 11 14
## 73 land rover auto 4 11 15
## 74 land rover auto 4 12 18
## 75 land rover auto 4 12 18
## 76 land rover auto 4 11 15
## 77 lincoln auto r 11 17
## 78 lincoln auto r 11 16
## 79 lincoln auto r 12 18
## 80 mercury auto 4 14 17
## 81 mercury auto 4 13 19
## 82 mercury auto 4 13 19
## 83 mercury auto 4 13 17
## 84 nissan auto 4 14 17
## 85 nissan manual 4 15 17
## 86 nissan auto 4 14 20
## 87 nissan auto 4 12 18
## 88 toyota manual 4 15 20
## 89 toyota auto 4 15 19
## 90 toyota manual 4 15 17
## 91 toyota auto 4 14 17
## 92 toyota auto 4 11 15
## 93 toyota auto 4 13 18
## 94 toyota manual 4 15 20
## 95 toyota manual 4 15 17
## 96 toyota auto 4 15 19
## 97 toyota manual 4 15 18
mpg1 %>% filter(cty>16)
## manufacturer trans drv cty hwy
## 1 audi auto f 18 29
## 2 audi manual f 21 29
## 3 audi manual f 20 31
## 4 audi auto f 21 30
## 5 audi manual f 18 26
## 6 audi auto f 18 27
## 7 audi manual 4 18 26
## 8 audi manual 4 20 28
## 9 audi auto 4 19 27
## 10 audi manual 4 17 25
## 11 audi auto 4 17 25
## 12 audi auto 4 17 25
## 13 chevrolet auto f 19 27
## 14 chevrolet auto f 22 30
## 15 chevrolet auto f 18 26
## 16 chevrolet auto f 18 29
## 17 chevrolet auto f 17 26
## 18 dodge auto f 18 24
## 19 dodge auto f 17 24
## 20 dodge auto f 17 24
## 21 dodge auto f 17 24
## 22 ford manual r 18 26
## 23 ford auto r 18 25
## 24 ford manual r 17 26
## 25 honda manual f 28 33
## 26 honda auto f 24 32
## 27 honda manual f 25 32
## 28 honda manual f 23 29
## 29 honda auto f 24 32
## 30 honda manual f 26 34
## 31 honda auto f 25 36
## 32 honda auto f 24 36
## 33 honda manual f 21 29
## 34 hyundai auto f 18 26
## 35 hyundai manual f 18 27
## 36 hyundai auto f 21 30
## 37 hyundai manual f 21 31
## 38 hyundai auto f 18 26
## 39 hyundai manual f 18 26
## 40 hyundai auto f 19 28
## 41 hyundai auto f 19 26
## 42 hyundai manual f 19 29
## 43 hyundai manual f 20 28
## 44 hyundai auto f 20 27
## 45 hyundai auto f 17 24
## 46 hyundai manual f 17 24
## 47 jeep auto 4 17 22
## 48 nissan manual f 21 29
## 49 nissan auto f 19 27
## 50 nissan auto f 23 31
## 51 nissan manual f 23 32
## 52 nissan manual f 19 27
## 53 nissan auto f 19 26
## 54 nissan auto f 18 26
## 55 nissan manual f 19 25
## 56 nissan auto f 19 25
## 57 pontiac auto f 18 26
## 58 pontiac auto f 17 27
## 59 pontiac auto f 18 28
## 60 subaru manual 4 18 25
## 61 subaru auto 4 18 24
## 62 subaru manual 4 20 27
## 63 subaru manual 4 19 25
## 64 subaru auto 4 20 26
## 65 subaru auto 4 18 23
## 66 subaru auto 4 21 26
## 67 subaru manual 4 19 26
## 68 subaru manual 4 19 26
## 69 subaru auto 4 19 26
## 70 subaru auto 4 20 25
## 71 subaru auto 4 20 27
## 72 subaru manual 4 19 25
## 73 subaru manual 4 20 27
## 74 toyota manual f 21 29
## 75 toyota auto f 21 27
## 76 toyota manual f 21 31
## 77 toyota auto f 21 31
## 78 toyota auto f 18 26
## 79 toyota manual f 18 26
## 80 toyota auto f 19 28
## 81 toyota auto f 21 27
## 82 toyota manual f 21 29
## 83 toyota manual f 21 31
## 84 toyota auto f 22 31
## 85 toyota auto f 18 26
## 86 toyota manual f 18 26
## 87 toyota auto f 18 27
## 88 toyota auto f 24 30
## 89 toyota auto f 24 33
## 90 toyota manual f 26 35
## 91 toyota manual f 28 37
## 92 toyota auto f 26 35
## 93 toyota manual 4 17 22
## 94 volkswagen manual f 21 29
## 95 volkswagen auto f 19 26
## 96 volkswagen manual f 21 29
## 97 volkswagen auto f 22 29
## 98 volkswagen manual f 17 24
## 99 volkswagen manual f 33 44
## 100 volkswagen manual f 21 29
## 101 volkswagen auto f 19 26
## 102 volkswagen auto f 22 29
## 103 volkswagen manual f 21 29
## 104 volkswagen auto f 21 29
## 105 volkswagen manual f 21 29
## 106 volkswagen manual f 17 24
## 107 volkswagen manual f 35 44
## 108 volkswagen auto f 29 41
## 109 volkswagen manual f 21 29
## 110 volkswagen auto f 19 26
## 111 volkswagen manual f 20 28
## 112 volkswagen auto f 20 29
## 113 volkswagen manual f 21 29
## 114 volkswagen auto f 18 29
## 115 volkswagen auto f 19 28
## 116 volkswagen manual f 21 29
## 117 volkswagen manual f 18 26
## 118 volkswagen auto f 17 26
quantile(mpg1$cty)
## 0% 25% 50% 75% 100%
## 9 14 17 19 35
mpg1 %>% filter(cty<=14)
## manufacturer trans drv cty hwy
## 1 chevrolet auto r 14 20
## 2 chevrolet auto r 11 15
## 3 chevrolet auto r 14 20
## 4 chevrolet auto r 13 17
## 5 chevrolet auto r 12 17
## 6 chevrolet auto 4 14 19
## 7 chevrolet auto 4 11 14
## 8 chevrolet auto 4 11 15
## 9 chevrolet auto 4 14 17
## 10 dodge auto f 11 17
## 11 dodge auto 4 14 18
## 12 dodge auto 4 13 17
## 13 dodge manual 4 14 17
## 14 dodge auto 4 14 19
## 15 dodge auto 4 14 19
## 16 dodge auto 4 9 12
## 17 dodge manual 4 11 17
## 18 dodge auto 4 11 15
## 19 dodge auto 4 13 17
## 20 dodge auto 4 13 17
## 21 dodge auto 4 9 12
## 22 dodge auto 4 13 17
## 23 dodge auto 4 11 16
## 24 dodge auto 4 13 18
## 25 dodge auto 4 11 15
## 26 dodge manual 4 12 16
## 27 dodge auto 4 9 12
## 28 dodge auto 4 13 17
## 29 dodge auto 4 13 17
## 30 dodge manual 4 12 16
## 31 dodge manual 4 9 12
## 32 dodge auto 4 11 15
## 33 dodge manual 4 11 16
## 34 dodge auto 4 13 17
## 35 dodge auto 4 11 15
## 36 ford auto r 11 17
## 37 ford auto r 11 17
## 38 ford auto r 12 18
## 39 ford auto 4 14 17
## 40 ford auto 4 14 17
## 41 ford auto 4 13 19
## 42 ford auto 4 13 19
## 43 ford auto 4 13 17
## 44 ford auto 4 14 17
## 45 ford manual 4 14 17
## 46 ford manual 4 13 16
## 47 ford auto 4 13 16
## 48 ford auto 4 13 17
## 49 ford auto 4 11 15
## 50 ford auto 4 13 17
## 51 ford manual r 14 20
## 52 jeep auto 4 14 17
## 53 jeep auto 4 9 12
## 54 jeep auto 4 14 19
## 55 jeep auto 4 13 18
## 56 jeep auto 4 11 14
## 57 land rover auto 4 11 15
## 58 land rover auto 4 12 18
## 59 land rover auto 4 12 18
## 60 land rover auto 4 11 15
## 61 lincoln auto r 11 17
## 62 lincoln auto r 11 16
## 63 lincoln auto r 12 18
## 64 mercury auto 4 14 17
## 65 mercury auto 4 13 19
## 66 mercury auto 4 13 19
## 67 mercury auto 4 13 17
## 68 nissan auto 4 14 17
## 69 nissan auto 4 14 20
## 70 nissan auto 4 12 18
## 71 toyota auto 4 14 17
## 72 toyota auto 4 11 15
## 73 toyota auto 4 13 18
median(mpg1$cty)
## [1] 17
mean(mpg1$cty)
## [1] 16.85897
mpg1 %>% filter(manufacturer=="hyundai"&cty>=16.85897|hwy<15)
## manufacturer trans drv cty hwy
## 1 chevrolet auto 4 11 14
## 2 dodge auto 4 9 12
## 3 dodge auto 4 9 12
## 4 dodge auto 4 9 12
## 5 dodge manual 4 9 12
## 6 hyundai auto f 18 26
## 7 hyundai manual f 18 27
## 8 hyundai auto f 21 30
## 9 hyundai manual f 21 31
## 10 hyundai auto f 18 26
## 11 hyundai manual f 18 26
## 12 hyundai auto f 19 28
## 13 hyundai auto f 19 26
## 14 hyundai manual f 19 29
## 15 hyundai manual f 20 28
## 16 hyundai auto f 20 27
## 17 hyundai auto f 17 24
## 18 hyundai manual f 17 24
## 19 jeep auto 4 9 12
## 20 jeep auto 4 11 14
mpg1 %>% filter(manufacturer=="honda"|manufacturer=="nissan")
## manufacturer trans drv cty hwy
## 1 honda manual f 28 33
## 2 honda auto f 24 32
## 3 honda manual f 25 32
## 4 honda manual f 23 29
## 5 honda auto f 24 32
## 6 honda manual f 26 34
## 7 honda auto f 25 36
## 8 honda auto f 24 36
## 9 honda manual f 21 29
## 10 nissan manual f 21 29
## 11 nissan auto f 19 27
## 12 nissan auto f 23 31
## 13 nissan manual f 23 32
## 14 nissan manual f 19 27
## 15 nissan auto f 19 26
## 16 nissan auto f 18 26
## 17 nissan manual f 19 25
## 18 nissan auto f 19 25
## 19 nissan auto 4 14 17
## 20 nissan manual 4 15 17
## 21 nissan auto 4 14 20
## 22 nissan auto 4 12 18
df<-mpg1 %>%select(manufacturer,cty) %>% filter(manufacturer=="hyundai"&cty>=16.85897)
head(df)
## manufacturer cty
## 1 hyundai 18
## 2 hyundai 18
## 3 hyundai 21
## 4 hyundai 21
## 5 hyundai 18
## 6 hyundai 18
dim(df)
## [1] 13 2
View(df)
mpg2<-mpg1 %>% mutate(total=cty+hwy)
head(mpg2)
## manufacturer trans drv cty hwy total
## 1 audi auto f 18 29 47
## 2 audi manual f 21 29 50
## 3 audi manual f 20 31 51
## 4 audi auto f 21 30 51
## 5 audi auto f 16 26 42
## 6 audi manual f 18 26 44
mpg3<-mpg1 %>% mutate(total=cty+hwy,mean=(cty+hwy)/2)
head(mpg3)
## manufacturer trans drv cty hwy total mean
## 1 audi auto f 18 29 47 23.5
## 2 audi manual f 21 29 50 25.0
## 3 audi manual f 20 31 51 25.5
## 4 audi auto f 21 30 51 25.5
## 5 audi auto f 16 26 42 21.0
## 6 audi manual f 18 26 44 22.0
mpg3 %>% group_by(trans) %>% summarise(m=mean(cty))
## # A tibble: 2 x 2
## trans m
## <chr> <dbl>
## 1 auto 16.0
## 2 manual 18.7
mpg3 %>% group_by(trans) %>% summarise(n=n())
## # A tibble: 2 x 2
## trans n
## <chr> <int>
## 1 auto 157
## 2 manual 77
mpg3 %>% group_by(trans) %>% summarise(n=n()) %>% mutate(total=sum(n),pct=n/total*100)
## # A tibble: 2 x 4
## trans n total pct
## <chr> <int> <int> <dbl>
## 1 auto 157 234 67.1
## 2 manual 77 234 32.9
a<-c(1,3,4,6,9)
ifelse(a%%2==0,"짝수","홀수")
## [1] "홀수" "홀수" "짝수" "짝수" "홀수"
mpg3 %>% mutate(cty_class=ifelse(cty>=16,"good","bad"))
## manufacturer trans drv cty hwy total mean cty_class
## 1 audi auto f 18 29 47 23.5 good
## 2 audi manual f 21 29 50 25.0 good
## 3 audi manual f 20 31 51 25.5 good
## 4 audi auto f 21 30 51 25.5 good
## 5 audi auto f 16 26 42 21.0 good
## 6 audi manual f 18 26 44 22.0 good
## 7 audi auto f 18 27 45 22.5 good
## 8 audi manual 4 18 26 44 22.0 good
## 9 audi auto 4 16 25 41 20.5 good
## 10 audi manual 4 20 28 48 24.0 good
## 11 audi auto 4 19 27 46 23.0 good
## 12 audi auto 4 15 25 40 20.0 bad
## 13 audi manual 4 17 25 42 21.0 good
## 14 audi auto 4 17 25 42 21.0 good
## 15 audi manual 4 15 25 40 20.0 bad
## 16 audi auto 4 15 24 39 19.5 bad
## 17 audi auto 4 17 25 42 21.0 good
## 18 audi auto 4 16 23 39 19.5 good
## 19 chevrolet auto r 14 20 34 17.0 bad
## 20 chevrolet auto r 11 15 26 13.0 bad
## 21 chevrolet auto r 14 20 34 17.0 bad
## 22 chevrolet auto r 13 17 30 15.0 bad
## 23 chevrolet auto r 12 17 29 14.5 bad
## 24 chevrolet manual r 16 26 42 21.0 good
## 25 chevrolet auto r 15 23 38 19.0 bad
## 26 chevrolet manual r 16 26 42 21.0 good
## 27 chevrolet auto r 15 25 40 20.0 bad
## 28 chevrolet manual r 15 24 39 19.5 bad
## 29 chevrolet auto 4 14 19 33 16.5 bad
## 30 chevrolet auto 4 11 14 25 12.5 bad
## 31 chevrolet auto 4 11 15 26 13.0 bad
## 32 chevrolet auto 4 14 17 31 15.5 bad
## 33 chevrolet auto f 19 27 46 23.0 good
## 34 chevrolet auto f 22 30 52 26.0 good
## 35 chevrolet auto f 18 26 44 22.0 good
## 36 chevrolet auto f 18 29 47 23.5 good
## 37 chevrolet auto f 17 26 43 21.5 good
## 38 dodge auto f 18 24 42 21.0 good
## 39 dodge auto f 17 24 41 20.5 good
## 40 dodge auto f 16 22 38 19.0 good
## 41 dodge auto f 16 22 38 19.0 good
## 42 dodge auto f 17 24 41 20.5 good
## 43 dodge auto f 17 24 41 20.5 good
## 44 dodge auto f 11 17 28 14.0 bad
## 45 dodge auto f 15 22 37 18.5 bad
## 46 dodge auto f 15 21 36 18.0 bad
## 47 dodge auto f 16 23 39 19.5 good
## 48 dodge auto f 16 23 39 19.5 good
## 49 dodge manual 4 15 19 34 17.0 bad
## 50 dodge auto 4 14 18 32 16.0 bad
## 51 dodge auto 4 13 17 30 15.0 bad
## 52 dodge manual 4 14 17 31 15.5 bad
## 53 dodge auto 4 14 19 33 16.5 bad
## 54 dodge auto 4 14 19 33 16.5 bad
## 55 dodge auto 4 9 12 21 10.5 bad
## 56 dodge manual 4 11 17 28 14.0 bad
## 57 dodge auto 4 11 15 26 13.0 bad
## 58 dodge auto 4 13 17 30 15.0 bad
## 59 dodge auto 4 13 17 30 15.0 bad
## 60 dodge auto 4 9 12 21 10.5 bad
## 61 dodge auto 4 13 17 30 15.0 bad
## 62 dodge auto 4 11 16 27 13.5 bad
## 63 dodge auto 4 13 18 31 15.5 bad
## 64 dodge auto 4 11 15 26 13.0 bad
## 65 dodge manual 4 12 16 28 14.0 bad
## 66 dodge auto 4 9 12 21 10.5 bad
## 67 dodge auto 4 13 17 30 15.0 bad
## 68 dodge auto 4 13 17 30 15.0 bad
## 69 dodge manual 4 12 16 28 14.0 bad
## 70 dodge manual 4 9 12 21 10.5 bad
## 71 dodge auto 4 11 15 26 13.0 bad
## 72 dodge manual 4 11 16 27 13.5 bad
## 73 dodge auto 4 13 17 30 15.0 bad
## 74 dodge auto 4 11 15 26 13.0 bad
## 75 ford auto r 11 17 28 14.0 bad
## 76 ford auto r 11 17 28 14.0 bad
## 77 ford auto r 12 18 30 15.0 bad
## 78 ford auto 4 14 17 31 15.5 bad
## 79 ford manual 4 15 19 34 17.0 bad
## 80 ford auto 4 14 17 31 15.5 bad
## 81 ford auto 4 13 19 32 16.0 bad
## 82 ford auto 4 13 19 32 16.0 bad
## 83 ford auto 4 13 17 30 15.0 bad
## 84 ford auto 4 14 17 31 15.5 bad
## 85 ford manual 4 14 17 31 15.5 bad
## 86 ford manual 4 13 16 29 14.5 bad
## 87 ford auto 4 13 16 29 14.5 bad
## 88 ford auto 4 13 17 30 15.0 bad
## 89 ford auto 4 11 15 26 13.0 bad
## 90 ford auto 4 13 17 30 15.0 bad
## 91 ford manual r 18 26 44 22.0 good
## 92 ford auto r 18 25 43 21.5 good
## 93 ford manual r 17 26 43 21.5 good
## 94 ford auto r 16 24 40 20.0 good
## 95 ford auto r 15 21 36 18.0 bad
## 96 ford manual r 15 22 37 18.5 bad
## 97 ford manual r 15 23 38 19.0 bad
## 98 ford auto r 15 22 37 18.5 bad
## 99 ford manual r 14 20 34 17.0 bad
## 100 honda manual f 28 33 61 30.5 good
## 101 honda auto f 24 32 56 28.0 good
## 102 honda manual f 25 32 57 28.5 good
## 103 honda manual f 23 29 52 26.0 good
## 104 honda auto f 24 32 56 28.0 good
## 105 honda manual f 26 34 60 30.0 good
## 106 honda auto f 25 36 61 30.5 good
## 107 honda auto f 24 36 60 30.0 good
## 108 honda manual f 21 29 50 25.0 good
## 109 hyundai auto f 18 26 44 22.0 good
## 110 hyundai manual f 18 27 45 22.5 good
## 111 hyundai auto f 21 30 51 25.5 good
## 112 hyundai manual f 21 31 52 26.0 good
## 113 hyundai auto f 18 26 44 22.0 good
## 114 hyundai manual f 18 26 44 22.0 good
## 115 hyundai auto f 19 28 47 23.5 good
## 116 hyundai auto f 19 26 45 22.5 good
## 117 hyundai manual f 19 29 48 24.0 good
## 118 hyundai manual f 20 28 48 24.0 good
## 119 hyundai auto f 20 27 47 23.5 good
## 120 hyundai auto f 17 24 41 20.5 good
## 121 hyundai manual f 16 24 40 20.0 good
## 122 hyundai manual f 17 24 41 20.5 good
## 123 jeep auto 4 17 22 39 19.5 good
## 124 jeep auto 4 15 19 34 17.0 bad
## 125 jeep auto 4 15 20 35 17.5 bad
## 126 jeep auto 4 14 17 31 15.5 bad
## 127 jeep auto 4 9 12 21 10.5 bad
## 128 jeep auto 4 14 19 33 16.5 bad
## 129 jeep auto 4 13 18 31 15.5 bad
## 130 jeep auto 4 11 14 25 12.5 bad
## 131 land rover auto 4 11 15 26 13.0 bad
## 132 land rover auto 4 12 18 30 15.0 bad
## 133 land rover auto 4 12 18 30 15.0 bad
## 134 land rover auto 4 11 15 26 13.0 bad
## 135 lincoln auto r 11 17 28 14.0 bad
## 136 lincoln auto r 11 16 27 13.5 bad
## 137 lincoln auto r 12 18 30 15.0 bad
## 138 mercury auto 4 14 17 31 15.5 bad
## 139 mercury auto 4 13 19 32 16.0 bad
## 140 mercury auto 4 13 19 32 16.0 bad
## 141 mercury auto 4 13 17 30 15.0 bad
## 142 nissan manual f 21 29 50 25.0 good
## 143 nissan auto f 19 27 46 23.0 good
## 144 nissan auto f 23 31 54 27.0 good
## 145 nissan manual f 23 32 55 27.5 good
## 146 nissan manual f 19 27 46 23.0 good
## 147 nissan auto f 19 26 45 22.5 good
## 148 nissan auto f 18 26 44 22.0 good
## 149 nissan manual f 19 25 44 22.0 good
## 150 nissan auto f 19 25 44 22.0 good
## 151 nissan auto 4 14 17 31 15.5 bad
## 152 nissan manual 4 15 17 32 16.0 bad
## 153 nissan auto 4 14 20 34 17.0 bad
## 154 nissan auto 4 12 18 30 15.0 bad
## 155 pontiac auto f 18 26 44 22.0 good
## 156 pontiac auto f 16 26 42 21.0 good
## 157 pontiac auto f 17 27 44 22.0 good
## 158 pontiac auto f 18 28 46 23.0 good
## 159 pontiac auto f 16 25 41 20.5 good
## 160 subaru manual 4 18 25 43 21.5 good
## 161 subaru auto 4 18 24 42 21.0 good
## 162 subaru manual 4 20 27 47 23.5 good
## 163 subaru manual 4 19 25 44 22.0 good
## 164 subaru auto 4 20 26 46 23.0 good
## 165 subaru auto 4 18 23 41 20.5 good
## 166 subaru auto 4 21 26 47 23.5 good
## 167 subaru manual 4 19 26 45 22.5 good
## 168 subaru manual 4 19 26 45 22.5 good
## 169 subaru auto 4 19 26 45 22.5 good
## 170 subaru auto 4 20 25 45 22.5 good
## 171 subaru auto 4 20 27 47 23.5 good
## 172 subaru manual 4 19 25 44 22.0 good
## 173 subaru manual 4 20 27 47 23.5 good
## 174 toyota manual 4 15 20 35 17.5 bad
## 175 toyota auto 4 16 20 36 18.0 good
## 176 toyota auto 4 15 19 34 17.0 bad
## 177 toyota manual 4 15 17 32 16.0 bad
## 178 toyota auto 4 16 20 36 18.0 good
## 179 toyota auto 4 14 17 31 15.5 bad
## 180 toyota manual f 21 29 50 25.0 good
## 181 toyota auto f 21 27 48 24.0 good
## 182 toyota manual f 21 31 52 26.0 good
## 183 toyota auto f 21 31 52 26.0 good
## 184 toyota auto f 18 26 44 22.0 good
## 185 toyota manual f 18 26 44 22.0 good
## 186 toyota auto f 19 28 47 23.5 good
## 187 toyota auto f 21 27 48 24.0 good
## 188 toyota manual f 21 29 50 25.0 good
## 189 toyota manual f 21 31 52 26.0 good
## 190 toyota auto f 22 31 53 26.5 good
## 191 toyota auto f 18 26 44 22.0 good
## 192 toyota manual f 18 26 44 22.0 good
## 193 toyota auto f 18 27 45 22.5 good
## 194 toyota auto f 24 30 54 27.0 good
## 195 toyota auto f 24 33 57 28.5 good
## 196 toyota manual f 26 35 61 30.5 good
## 197 toyota manual f 28 37 65 32.5 good
## 198 toyota auto f 26 35 61 30.5 good
## 199 toyota auto 4 11 15 26 13.0 bad
## 200 toyota auto 4 13 18 31 15.5 bad
## 201 toyota manual 4 15 20 35 17.5 bad
## 202 toyota auto 4 16 20 36 18.0 good
## 203 toyota manual 4 17 22 39 19.5 good
## 204 toyota manual 4 15 17 32 16.0 bad
## 205 toyota auto 4 15 19 34 17.0 bad
## 206 toyota manual 4 15 18 33 16.5 bad
## 207 toyota auto 4 16 20 36 18.0 good
## 208 volkswagen manual f 21 29 50 25.0 good
## 209 volkswagen auto f 19 26 45 22.5 good
## 210 volkswagen manual f 21 29 50 25.0 good
## 211 volkswagen auto f 22 29 51 25.5 good
## 212 volkswagen manual f 17 24 41 20.5 good
## 213 volkswagen manual f 33 44 77 38.5 good
## 214 volkswagen manual f 21 29 50 25.0 good
## 215 volkswagen auto f 19 26 45 22.5 good
## 216 volkswagen auto f 22 29 51 25.5 good
## 217 volkswagen manual f 21 29 50 25.0 good
## 218 volkswagen auto f 21 29 50 25.0 good
## 219 volkswagen manual f 21 29 50 25.0 good
## 220 volkswagen auto f 16 23 39 19.5 good
## 221 volkswagen manual f 17 24 41 20.5 good
## 222 volkswagen manual f 35 44 79 39.5 good
## 223 volkswagen auto f 29 41 70 35.0 good
## 224 volkswagen manual f 21 29 50 25.0 good
## 225 volkswagen auto f 19 26 45 22.5 good
## 226 volkswagen manual f 20 28 48 24.0 good
## 227 volkswagen auto f 20 29 49 24.5 good
## 228 volkswagen manual f 21 29 50 25.0 good
## 229 volkswagen auto f 18 29 47 23.5 good
## 230 volkswagen auto f 19 28 47 23.5 good
## 231 volkswagen manual f 21 29 50 25.0 good
## 232 volkswagen auto f 16 26 42 21.0 good
## 233 volkswagen manual f 18 26 44 22.0 good
## 234 volkswagen auto f 17 26 43 21.5 good
mpg3 %>% mutate(hwy_class=ifelse(hwy>=27,"best",
ifelse(hwy>=24,"good", ifelse(hwy>=18,"normal","bad")))) %>%
group_by(hwy_class) %>% summarise(m=mean(cty)) %>%
arrange(desc(m))
## # A tibble: 4 x 2
## hwy_class m
## <chr> <dbl>
## 1 best 21.7
## 2 good 17.6
## 3 normal 14.6
## 4 bad 12.1
mpg2<-read.csv("c:/R/mpg2.csv")
mpg3<-read.csv("c:/R/mpg3.csv")
mpg2
## id manufacturer cty
## 1 1 audi 18
## 2 2 audi 21
## 3 3 audi 20
head(mpg3)
## id hwy
## 1 1 29
## 2 4 26
## 3 5 26
inner_join(mpg2,mpg3,by="id")
## id manufacturer cty hwy
## 1 1 audi 18 29
left_join(mpg2,mpg3,by="id")
## id manufacturer cty hwy
## 1 1 audi 18 29
## 2 2 audi 21 NA
## 3 3 audi 20 NA
full_join(mpg2,mpg3,by="id")
## id manufacturer cty hwy
## 1 1 audi 18 29
## 2 2 audi 21 NA
## 3 3 audi 20 NA
## 4 4 <NA> NA 26
## 5 5 <NA> NA 26