ls()
## character(0)
rm(list = ls())
ls()
## character(0)
library(dplyr)
##
## 다음의 패키지를 부착합니다: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
df <- read.csv('ta1.csv', fileEncoding = 'euc-kr')
#View(df)
head(df)
## 가해자연령층별.1. 월별.1. X2022 X2022.1 X2022.2
## 1 가해자연령층별(1) 월별(1) 사고건수 (건) 사망자수 (명) 부상자수 (명)
## 2 20세이하 전체 6508 72 8863
## 3 20세이하 1월 435 6 633
## 4 20세이하 2월 357 2 505
## 5 20세이하 3월 473 3 678
## 6 20세이하 4월 580 7 810
names(df)
## [1] "가해자연령층별.1." "월별.1." "X2022"
## [4] "X2022.1" "X2022.2"
df %>% rename(month=월별.1., 사고건수=X2022, 사망자수=X2022.1,
연령층=가해자연령층별.1., 부상자수=X2022.2)
## 연령층 month 사고건수 사망자수 부상자수
## 1 가해자연령층별(1) 월별(1) 사고건수 (건) 사망자수 (명) 부상자수 (명)
## 2 20세이하 전체 6508 72 8863
## 3 20세이하 1월 435 6 633
## 4 20세이하 2월 357 2 505
## 5 20세이하 3월 473 3 678
## 6 20세이하 4월 580 7 810
## 7 20세이하 5월 713 6 955
## 8 20세이하 6월 668 5 877
## 9 20세이하 7월 639 6 847
## 10 20세이하 8월 557 7 735
## 11 20세이하 9월 609 8 808
## 12 20세이하 10월 618 8 805
## 13 20세이하 11월 532 8 724
## 14 20세이하 12월 327 6 486
## 15 21-30세 전체 26904 335 39706
## 16 21-30세 1월 2307 31 3470
## 17 21-30세 2월 1787 22 2614
## 18 21-30세 3월 1950 26 2793
## 19 21-30세 4월 2310 16 3460
## 20 21-30세 5월 2503 29 3719
## 21 21-30세 6월 2182 29 3209
## 22 21-30세 7월 2368 32 3546
## 23 21-30세 8월 2322 30 3382
## 24 21-30세 9월 2276 28 3419
## 25 21-30세 10월 2376 38 3442
## 26 21-30세 11월 2228 26 3209
## 27 21-30세 12월 2295 28 3443
## 28 31-40세 전체 28402 317 41287
## 29 31-40세 1월 2336 28 3418
## 30 31-40세 2월 2001 30 2903
## 31 31-40세 3월 1969 19 2820
## 32 31-40세 4월 2409 29 3534
## 33 31-40세 5월 2525 26 3613
## 34 31-40세 6월 2341 22 3446
## 35 31-40세 7월 2463 22 3615
## 36 31-40세 8월 2330 30 3428
## 37 31-40세 9월 2268 25 3285
## 38 31-40세 10월 2589 32 3750
## 39 31-40세 11월 2489 23 3579
## 40 31-40세 12월 2682 31 3896
## 41 41-50세 전체 33915 409 48389
## 42 41-50세 1월 2939 33 4156
## 43 41-50세 2월 2197 20 3086
## 44 41-50세 3월 2391 27 3302
## 45 41-50세 4월 2805 29 4013
## 46 41-50세 5월 3096 34 4321
## 47 41-50세 6월 2760 37 3878
## 48 41-50세 7월 2930 44 4296
## 49 41-50세 8월 2776 30 4056
## 50 41-50세 9월 2900 37 4255
## 51 41-50세 10월 3083 38 4543
## 52 41-50세 11월 2994 43 4182
## 53 41-50세 12월 3044 37 4301
## 54 51-60세 전체 44581 570 63632
## 55 51-60세 1월 3683 40 5243
## 56 51-60세 2월 2849 34 4085
## 57 51-60세 3월 3191 37 4345
## 58 51-60세 4월 3772 56 5385
## 59 51-60세 5월 3989 47 5745
## 60 51-60세 6월 3733 50 5266
## 61 51-60세 7월 3747 49 5414
## 62 51-60세 8월 3677 44 5400
## 63 51-60세 9월 3872 61 5485
## 64 51-60세 10월 4156 53 6083
## 65 51-60세 11월 4001 52 5554
## 66 51-60세 12월 3911 47 5627
## 67 61-64세 전체 19160 297 27566
## 68 61-64세 1월 1494 19 2178
## 69 61-64세 2월 1112 16 1578
## 70 61-64세 3월 1211 23 1658
## 71 61-64세 4월 1535 20 2309
## 72 61-64세 5월 1735 23 2503
## 73 61-64세 6월 1691 29 2432
## 74 61-64세 7월 1705 24 2472
## 75 61-64세 8월 1681 29 2477
## 76 61-64세 9월 1751 27 2464
## 77 61-64세 10월 1855 43 2679
## 78 61-64세 11월 1703 27 2430
## 79 61-64세 12월 1687 17 2386
## 80 65-70세 전체 19399 349 27792
## 81 65-70세 1월 1395 17 1973
## 82 65-70세 2월 1042 14 1478
## 83 65-70세 3월 1234 26 1660
## 84 65-70세 4월 1554 26 2229
## 85 65-70세 5월 1792 27 2613
## 86 65-70세 6월 1633 25 2339
## 87 65-70세 7월 1724 34 2512
## 88 65-70세 8월 1614 38 2333
## 89 65-70세 9월 1777 32 2528
## 90 65-70세 10월 1960 31 2816
## 91 65-70세 11월 1945 37 2769
## 92 65-70세 12월 1729 42 2542
## 93 71세이상 전체 15253 386 21489
## 94 71세이상 1월 1074 19 1453
## 95 71세이상 2월 845 23 1148
## 96 71세이상 3월 1018 22 1393
## 97 71세이상 4월 1296 33 1828
## 98 71세이상 5월 1453 43 2013
## 99 71세이상 6월 1257 34 1740
## 100 71세이상 7월 1324 30 1877
## 101 71세이상 8월 1306 34 1865
## 102 71세이상 9월 1483 39 2095
## 103 71세이상 10월 1647 42 2396
## 104 71세이상 11월 1443 41 1994
## 105 71세이상 12월 1107 26 1687
## 106 불명 전체 2714 0 3079
## 107 불명 1월 231 0 267
## 108 불명 2월 172 0 191
## 109 불명 3월 183 0 204
## 110 불명 4월 211 0 231
## 111 불명 5월 259 0 290
## 112 불명 6월 216 0 242
## 113 불명 7월 215 0 233
## 114 불명 8월 233 0 278
## 115 불명 9월 280 0 305
## 116 불명 10월 224 0 261
## 117 불명 11월 243 0 302
## 118 불명 12월 247 0 275
df1 <- df %>% rename(month=월별.1., 사고건수=X2022, 사망자수=X2022.1,
연령층=가해자연령층별.1., 부상자수=X2022.2)
head(df1)
## 연령층 month 사고건수 사망자수 부상자수
## 1 가해자연령층별(1) 월별(1) 사고건수 (건) 사망자수 (명) 부상자수 (명)
## 2 20세이하 전체 6508 72 8863
## 3 20세이하 1월 435 6 633
## 4 20세이하 2월 357 2 505
## 5 20세이하 3월 473 3 678
## 6 20세이하 4월 580 7 810
df2 <- df1 %>% slice(-1)
head(df2)
## 연령층 month 사고건수 사망자수 부상자수
## 1 20세이하 전체 6508 72 8863
## 2 20세이하 1월 435 6 633
## 3 20세이하 2월 357 2 505
## 4 20세이하 3월 473 3 678
## 5 20세이하 4월 580 7 810
## 6 20세이하 5월 713 6 955
table(df2$month)
##
## 10월 11월 12월 1월 2월 3월 4월 5월 6월 7월 8월 9월 전체
## 9 9 9 9 9 9 9 9 9 9 9 9 9
df2 %>% filter(month!='전체')
## 연령층 month 사고건수 사망자수 부상자수
## 1 20세이하 1월 435 6 633
## 2 20세이하 2월 357 2 505
## 3 20세이하 3월 473 3 678
## 4 20세이하 4월 580 7 810
## 5 20세이하 5월 713 6 955
## 6 20세이하 6월 668 5 877
## 7 20세이하 7월 639 6 847
## 8 20세이하 8월 557 7 735
## 9 20세이하 9월 609 8 808
## 10 20세이하 10월 618 8 805
## 11 20세이하 11월 532 8 724
## 12 20세이하 12월 327 6 486
## 13 21-30세 1월 2307 31 3470
## 14 21-30세 2월 1787 22 2614
## 15 21-30세 3월 1950 26 2793
## 16 21-30세 4월 2310 16 3460
## 17 21-30세 5월 2503 29 3719
## 18 21-30세 6월 2182 29 3209
## 19 21-30세 7월 2368 32 3546
## 20 21-30세 8월 2322 30 3382
## 21 21-30세 9월 2276 28 3419
## 22 21-30세 10월 2376 38 3442
## 23 21-30세 11월 2228 26 3209
## 24 21-30세 12월 2295 28 3443
## 25 31-40세 1월 2336 28 3418
## 26 31-40세 2월 2001 30 2903
## 27 31-40세 3월 1969 19 2820
## 28 31-40세 4월 2409 29 3534
## 29 31-40세 5월 2525 26 3613
## 30 31-40세 6월 2341 22 3446
## 31 31-40세 7월 2463 22 3615
## 32 31-40세 8월 2330 30 3428
## 33 31-40세 9월 2268 25 3285
## 34 31-40세 10월 2589 32 3750
## 35 31-40세 11월 2489 23 3579
## 36 31-40세 12월 2682 31 3896
## 37 41-50세 1월 2939 33 4156
## 38 41-50세 2월 2197 20 3086
## 39 41-50세 3월 2391 27 3302
## 40 41-50세 4월 2805 29 4013
## 41 41-50세 5월 3096 34 4321
## 42 41-50세 6월 2760 37 3878
## 43 41-50세 7월 2930 44 4296
## 44 41-50세 8월 2776 30 4056
## 45 41-50세 9월 2900 37 4255
## 46 41-50세 10월 3083 38 4543
## 47 41-50세 11월 2994 43 4182
## 48 41-50세 12월 3044 37 4301
## 49 51-60세 1월 3683 40 5243
## 50 51-60세 2월 2849 34 4085
## 51 51-60세 3월 3191 37 4345
## 52 51-60세 4월 3772 56 5385
## 53 51-60세 5월 3989 47 5745
## 54 51-60세 6월 3733 50 5266
## 55 51-60세 7월 3747 49 5414
## 56 51-60세 8월 3677 44 5400
## 57 51-60세 9월 3872 61 5485
## 58 51-60세 10월 4156 53 6083
## 59 51-60세 11월 4001 52 5554
## 60 51-60세 12월 3911 47 5627
## 61 61-64세 1월 1494 19 2178
## 62 61-64세 2월 1112 16 1578
## 63 61-64세 3월 1211 23 1658
## 64 61-64세 4월 1535 20 2309
## 65 61-64세 5월 1735 23 2503
## 66 61-64세 6월 1691 29 2432
## 67 61-64세 7월 1705 24 2472
## 68 61-64세 8월 1681 29 2477
## 69 61-64세 9월 1751 27 2464
## 70 61-64세 10월 1855 43 2679
## 71 61-64세 11월 1703 27 2430
## 72 61-64세 12월 1687 17 2386
## 73 65-70세 1월 1395 17 1973
## 74 65-70세 2월 1042 14 1478
## 75 65-70세 3월 1234 26 1660
## 76 65-70세 4월 1554 26 2229
## 77 65-70세 5월 1792 27 2613
## 78 65-70세 6월 1633 25 2339
## 79 65-70세 7월 1724 34 2512
## 80 65-70세 8월 1614 38 2333
## 81 65-70세 9월 1777 32 2528
## 82 65-70세 10월 1960 31 2816
## 83 65-70세 11월 1945 37 2769
## 84 65-70세 12월 1729 42 2542
## 85 71세이상 1월 1074 19 1453
## 86 71세이상 2월 845 23 1148
## 87 71세이상 3월 1018 22 1393
## 88 71세이상 4월 1296 33 1828
## 89 71세이상 5월 1453 43 2013
## 90 71세이상 6월 1257 34 1740
## 91 71세이상 7월 1324 30 1877
## 92 71세이상 8월 1306 34 1865
## 93 71세이상 9월 1483 39 2095
## 94 71세이상 10월 1647 42 2396
## 95 71세이상 11월 1443 41 1994
## 96 71세이상 12월 1107 26 1687
## 97 불명 1월 231 0 267
## 98 불명 2월 172 0 191
## 99 불명 3월 183 0 204
## 100 불명 4월 211 0 231
## 101 불명 5월 259 0 290
## 102 불명 6월 216 0 242
## 103 불명 7월 215 0 233
## 104 불명 8월 233 0 278
## 105 불명 9월 280 0 305
## 106 불명 10월 224 0 261
## 107 불명 11월 243 0 302
## 108 불명 12월 247 0 275
df2 %>% filter(month!='전체') %>% glimpse()
## Rows: 108
## Columns: 5
## $ 연령층 <chr> "20세이하", "20세이하", "20세이하", "20세이하", "20세이하", "…
## $ month <chr> "1월", "2월", "3월", "4월", "5월", "6월", "7월", "8월", "9월"…
## $ 사고건수 <chr> "435", "357", "473", "580", "713", "668", "639", "557", "609"…
## $ 사망자수 <chr> "6", "2", "3", "7", "6", "5", "6", "7", "8", "8", "8", "6", "…
## $ 부상자수 <chr> "633", "505", "678", "810", "955", "877", "847", "735", "808"…
df2$사고건수 <- as.numeric(df2$사고건수)
glimpse(df2)
## Rows: 117
## Columns: 5
## $ 연령층 <chr> "20세이하", "20세이하", "20세이하", "20세이하", "20세이하", "…
## $ month <chr> "전체", "1월", "2월", "3월", "4월", "5월", "6월", "7월", "8월…
## $ 사고건수 <dbl> 6508, 435, 357, 473, 580, 713, 668, 639, 557, 609, 618, 532, …
## $ 사망자수 <chr> "72", "6", "2", "3", "7", "6", "5", "6", "7", "8", "8", "8", …
## $ 부상자수 <chr> "8863", "633", "505", "678", "810", "955", "877", "847", "735…
#install.packages('gapminder')
library(gapminder)
glimpse(gapminder)
## Rows: 1,704
## Columns: 6
## $ country <fct> "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", …
## $ continent <fct> Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, …
## $ year <int> 1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, …
## $ lifeExp <dbl> 28.801, 30.332, 31.997, 34.020, 36.088, 38.438, 39.854, 40.8…
## $ pop <int> 8425333, 9240934, 10267083, 11537966, 13079460, 14880372, 12…
## $ gdpPercap <dbl> 779.4453, 820.8530, 853.1007, 836.1971, 739.9811, 786.1134, …
y <- gapminder %>% group_by(year, continent) %>% summarise(c_pop=sum(pop))
## `summarise()` has grouped output by 'year'. You can override using the
## `.groups` argument.
head(y, 20)
## # A tibble: 20 × 3
## # Groups: year [4]
## year continent c_pop
## <int> <fct> <dbl>
## 1 1952 Africa 237640501
## 2 1952 Americas 345152446
## 3 1952 Asia 1395357351
## 4 1952 Europe 418120846
## 5 1952 Oceania 10686006
## 6 1957 Africa 264837738
## 7 1957 Americas 386953916
## 8 1957 Asia 1562780599
## 9 1957 Europe 437890351
## 10 1957 Oceania 11941976
## 11 1962 Africa 296516865
## 12 1962 Americas 433270254
## 13 1962 Asia 1696357182
## 14 1962 Europe 460355155
## 15 1962 Oceania 13283518
## 16 1967 Africa 335289489
## 17 1967 Americas 480746623
## 18 1967 Asia 1905662900
## 19 1967 Europe 481178958
## 20 1967 Oceania 14600414
plot(y$year, y$c_pop)

plot(gapminder$gdpPercap, gapminder$lifeExp, col=gapminder$lifeExp)
legend('bottomright',legend = levels((gapminder$continent)),
pch = c(1:length(levels(gapminder$continent))),
col = c(1:length(levels(gapminder$continent))))

plot(log10(gapminder$gdpPercap), gapminder$lifeExp, col = gapminder$continent)
legend('bottomright', legend = levels((gapminder$continent)),
pch = c(1:length(levels(gapminder$continent))),
col = c(1:length(levels(gapminder$continent))))
library(ggplot2)

ggplot(gapminder, aes(x=gdpPercap, y=lifeExp, col=continent)) +
geom_point() + scale_x_log10()

ggplot(gapminder, aes(x=gdpPercap, y=lifeExp, col=continent, size=pop)) +
geom_point() + scale_x_log10()

ggplot(gapminder, aes(x=gdpPercap, y=lifeExp, col=continent, size=pop)) +
geom_point(alpha=0.5) + scale_x_log10()

ggplot(gapminder, aes(x=gdpPercap, y=lifeExp, col=continent, size=pop)) +
geom_point(alpha=0.5) + scale_x_log10() + facet_wrap(~year)

gapminder %>% filter(year==1952 & continent=='Asia') %>%
ggplot(aes(reorder(country, pop), pop)) + geom_bar(stat = 'identity') +coord_flip()

gapminder %>% filter(country=='Korea, Rep.') %>% ggplot(aes(year, lifeExp, col=country)) +
geom_point() + geom_line()

gapminder %>% ggplot(aes(x=year, y=lifeExp, col=continent)) +
geom_point(alpha=0.2) + geom_smooth()
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

x = filter(gapminder, year==1952)
hist(x$lifeExp, main='<<<< Histogram of lifeExp 1952 >>>>')

x %>% ggplot(aes(continent, lifeExp)) + geom_boxplot()

getwd()
## [1] "C:/data"
library(dplyr)
data(cars)
glimpse(cars)
## Rows: 50
## Columns: 2
## $ speed <dbl> 4, 4, 7, 7, 8, 9, 10, 10, 10, 11, 11, 12, 12, 12, 12, 13, 13, 13…
## $ dist <dbl> 2, 10, 4, 22, 16, 10, 18, 26, 34, 17, 28, 14, 20, 24, 28, 26, 34…
summarise(cars)
## data frame with 0 columns and 1 row
cars1 <- cars %>% mutate(violation=ifelse(cars$speed>15.4,'fast','slow'))
cars1
## speed dist violation
## 1 4 2 slow
## 2 4 10 slow
## 3 7 4 slow
## 4 7 22 slow
## 5 8 16 slow
## 6 9 10 slow
## 7 10 18 slow
## 8 10 26 slow
## 9 10 34 slow
## 10 11 17 slow
## 11 11 28 slow
## 12 12 14 slow
## 13 12 20 slow
## 14 12 24 slow
## 15 12 28 slow
## 16 13 26 slow
## 17 13 34 slow
## 18 13 34 slow
## 19 13 46 slow
## 20 14 26 slow
## 21 14 36 slow
## 22 14 60 slow
## 23 14 80 slow
## 24 15 20 slow
## 25 15 26 slow
## 26 15 54 slow
## 27 16 32 fast
## 28 16 40 fast
## 29 17 32 fast
## 30 17 40 fast
## 31 17 50 fast
## 32 18 42 fast
## 33 18 56 fast
## 34 18 76 fast
## 35 18 84 fast
## 36 19 36 fast
## 37 19 46 fast
## 38 19 68 fast
## 39 20 32 fast
## 40 20 48 fast
## 41 20 52 fast
## 42 20 56 fast
## 43 20 64 fast
## 44 22 66 fast
## 45 23 54 fast
## 46 24 70 fast
## 47 24 92 fast
## 48 24 93 fast
## 49 24 120 fast
## 50 25 85 fast
glimpse(cars1)
## Rows: 50
## Columns: 3
## $ speed <dbl> 4, 4, 7, 7, 8, 9, 10, 10, 10, 11, 11, 12, 12, 12, 12, 13, 13…
## $ dist <dbl> 2, 10, 4, 22, 16, 10, 18, 26, 34, 17, 28, 14, 20, 24, 28, 26…
## $ violation <chr> "slow", "slow", "slow", "slow", "slow", "slow", "slow", "slo…
cars1$violation <- factor(cars1$violation)
glimpse(cars1)
## Rows: 50
## Columns: 3
## $ speed <dbl> 4, 4, 7, 7, 8, 9, 10, 10, 10, 11, 11, 12, 12, 12, 12, 13, 13…
## $ dist <dbl> 2, 10, 4, 22, 16, 10, 18, 26, 34, 17, 28, 14, 20, 24, 28, 26…
## $ violation <fct> slow, slow, slow, slow, slow, slow, slow, slow, slow, slow, …
cars1 %>% group_by(violation) %>% summarise(p=mean(speed))
## # A tibble: 2 × 2
## violation p
## <fct> <dbl>
## 1 fast 19.9
## 2 slow 11.2
# hflights
#install.packages('hflights')
library(hflights)
summarise(hflights)
## data frame with 0 columns and 1 row
glimpse(hflights)
## Rows: 227,496
## Columns: 21
## $ Year <int> 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011…
## $ Month <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
## $ DayofMonth <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1…
## $ DayOfWeek <int> 6, 7, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 1, 2…
## $ DepTime <int> 1400, 1401, 1352, 1403, 1405, 1359, 1359, 1355, 1443…
## $ ArrTime <int> 1500, 1501, 1502, 1513, 1507, 1503, 1509, 1454, 1554…
## $ UniqueCarrier <chr> "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA"…
## $ FlightNum <int> 428, 428, 428, 428, 428, 428, 428, 428, 428, 428, 42…
## $ TailNum <chr> "N576AA", "N557AA", "N541AA", "N403AA", "N492AA", "N…
## $ ActualElapsedTime <int> 60, 60, 70, 70, 62, 64, 70, 59, 71, 70, 70, 56, 63, …
## $ AirTime <int> 40, 45, 48, 39, 44, 45, 43, 40, 41, 45, 42, 41, 44, …
## $ ArrDelay <int> -10, -9, -8, 3, -3, -7, -1, -16, 44, 43, 29, 5, -9, …
## $ DepDelay <int> 0, 1, -8, 3, 5, -1, -1, -5, 43, 43, 29, 19, -2, -3, …
## $ Origin <chr> "IAH", "IAH", "IAH", "IAH", "IAH", "IAH", "IAH", "IA…
## $ Dest <chr> "DFW", "DFW", "DFW", "DFW", "DFW", "DFW", "DFW", "DF…
## $ Distance <int> 224, 224, 224, 224, 224, 224, 224, 224, 224, 224, 22…
## $ TaxiIn <int> 7, 6, 5, 9, 9, 6, 12, 7, 8, 6, 8, 4, 6, 5, 6, 12, 8,…
## $ TaxiOut <int> 13, 9, 17, 22, 9, 13, 15, 12, 22, 19, 20, 11, 13, 15…
## $ Cancelled <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
## $ CancellationCode <chr> "", "", "", "", "", "", "", "", "", "", "", "", "", …
## $ Diverted <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
# Month 빈도분석
hflights %>% count(Month)
## Month n
## 1 1 18910
## 2 2 17128
## 3 3 19470
## 4 4 18593
## 5 5 19172
## 6 6 19600
## 7 7 20548
## 8 8 20176
## 9 9 18065
## 10 10 18696
## 11 11 18021
## 12 12 19117
# Month 가 2월 8월 데이터 추출
hflights %>% filter(Month==2 & Month==8) %>% dim()
## [1] 0 21
hflights %>% count(Dest) %>% arrange(n)
## Dest n
## 1 AGS 1
## 2 BPT 3
## 3 GRK 42
## 4 GUC 86
## 5 PSP 106
## 6 BKG 110
## 7 EGE 110
## 8 HDN 110
## 9 ANC 125
## 10 ASE 125
## 11 MTJ 164
## 12 RNO 243
## 13 MLU 292
## 14 HOB 309
## 15 AVL 350
## 16 CRW 357
## 17 LCH 364
## 18 SJU 391
## 19 HNL 402
## 20 GJT 403
## 21 CID 410
## 22 DAY 451
## 23 BFL 504
## 24 CAE 561
## 25 LEX 584
## 26 GSO 630
## 27 DSM 647
## 28 GRR 677
## 29 OAK 690
## 30 JFK 695
## 31 ORF 717
## 32 AEX 724
## 33 ECP 729
## 34 SHV 787
## 35 SAV 863
## 36 VPS 880
## 37 SJC 885
## 38 RIC 900
## 39 HSV 923
## 40 RSW 948
## 41 ONT 952
## 42 SMF 1014
## 43 GSP 1123
## 44 MFE 1128
## 45 XNA 1172
## 46 LRD 1188
## 47 CHS 1200
## 48 TYS 1210
## 49 PDX 1235
## 50 PBI 1253
## 51 SDF 1279
## 52 AMA 1297
## 53 LBB 1333
## 54 CMH 1348
## 55 ICT 1517
## 56 CVG 1535
## 57 PNS 1539
## 58 TUS 1565
## 59 LIT 1579
## 60 MKE 1588
## 61 GPT 1618
## 62 COS 1657
## 63 SNA 1661
## 64 PIT 1664
## 65 MOB 1674
## 66 BRO 1692
## 67 RDU 1740
## 68 IND 1750
## 69 BOS 1752
## 70 BTR 1762
## 71 IAD 1980
## 72 MSP 2010
## 73 JAN 2011
## 74 SLC 2033
## 75 OMA 2044
## 76 MDW 2094
## 77 JAX 2135
## 78 CLE 2140
## 79 MAF 2306
## 80 LFT 2313
## 81 PHL 2367
## 82 MEM 2399
## 83 FLL 2462
## 84 MIA 2463
## 85 STL 2509
## 86 BWI 2551
## 87 DTW 2601
## 88 SEA 2615
## 89 DCA 2699
## 90 LGA 2730
## 91 BHM 2736
## 92 ABQ 2812
## 93 SFO 2818
## 94 TUL 2924
## 95 SAN 2936
## 96 ELP 3036
## 97 TPA 3085
## 98 OKC 3170
## 99 MCI 3174
## 100 BNA 3481
## 101 MCO 3687
## 102 HRL 3983
## 103 LAS 4082
## 104 EWR 4314
## 105 CLT 4735
## 106 CRP 4813
## 107 SAT 4893
## 108 AUS 5022
## 109 PHX 5096
## 110 ORD 5748
## 111 DEN 5920
## 112 LAX 6064
## 113 DFW 6653
## 114 MSY 6823
## 115 ATL 7886
## 116 DAL 9820
hflights %>% count(Dest) %>% arrange(desc(n))
## Dest n
## 1 DAL 9820
## 2 ATL 7886
## 3 MSY 6823
## 4 DFW 6653
## 5 LAX 6064
## 6 DEN 5920
## 7 ORD 5748
## 8 PHX 5096
## 9 AUS 5022
## 10 SAT 4893
## 11 CRP 4813
## 12 CLT 4735
## 13 EWR 4314
## 14 LAS 4082
## 15 HRL 3983
## 16 MCO 3687
## 17 BNA 3481
## 18 MCI 3174
## 19 OKC 3170
## 20 TPA 3085
## 21 ELP 3036
## 22 SAN 2936
## 23 TUL 2924
## 24 SFO 2818
## 25 ABQ 2812
## 26 BHM 2736
## 27 LGA 2730
## 28 DCA 2699
## 29 SEA 2615
## 30 DTW 2601
## 31 BWI 2551
## 32 STL 2509
## 33 MIA 2463
## 34 FLL 2462
## 35 MEM 2399
## 36 PHL 2367
## 37 LFT 2313
## 38 MAF 2306
## 39 CLE 2140
## 40 JAX 2135
## 41 MDW 2094
## 42 OMA 2044
## 43 SLC 2033
## 44 JAN 2011
## 45 MSP 2010
## 46 IAD 1980
## 47 BTR 1762
## 48 BOS 1752
## 49 IND 1750
## 50 RDU 1740
## 51 BRO 1692
## 52 MOB 1674
## 53 PIT 1664
## 54 SNA 1661
## 55 COS 1657
## 56 GPT 1618
## 57 MKE 1588
## 58 LIT 1579
## 59 TUS 1565
## 60 PNS 1539
## 61 CVG 1535
## 62 ICT 1517
## 63 CMH 1348
## 64 LBB 1333
## 65 AMA 1297
## 66 SDF 1279
## 67 PBI 1253
## 68 PDX 1235
## 69 TYS 1210
## 70 CHS 1200
## 71 LRD 1188
## 72 XNA 1172
## 73 MFE 1128
## 74 GSP 1123
## 75 SMF 1014
## 76 ONT 952
## 77 RSW 948
## 78 HSV 923
## 79 RIC 900
## 80 SJC 885
## 81 VPS 880
## 82 SAV 863
## 83 SHV 787
## 84 ECP 729
## 85 AEX 724
## 86 ORF 717
## 87 JFK 695
## 88 OAK 690
## 89 GRR 677
## 90 DSM 647
## 91 GSO 630
## 92 LEX 584
## 93 CAE 561
## 94 BFL 504
## 95 DAY 451
## 96 CID 410
## 97 GJT 403
## 98 HNL 402
## 99 SJU 391
## 100 LCH 364
## 101 CRW 357
## 102 AVL 350
## 103 HOB 309
## 104 MLU 292
## 105 RNO 243
## 106 MTJ 164
## 107 ANC 125
## 108 ASE 125
## 109 BKG 110
## 110 EGE 110
## 111 HDN 110
## 112 PSP 106
## 113 GUC 86
## 114 GRK 42
## 115 BPT 3
## 116 AGS 1
# gapminder data
library(gapminder)
glimpse(gapminder)
## Rows: 1,704
## Columns: 6
## $ country <fct> "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", …
## $ continent <fct> Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, …
## $ year <int> 1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, …
## $ lifeExp <dbl> 28.801, 30.332, 31.997, 34.020, 36.088, 38.438, 39.854, 40.8…
## $ pop <int> 8425333, 9240934, 10267083, 11537966, 13079460, 14880372, 12…
## $ gdpPercap <dbl> 779.4453, 820.8530, 853.1007, 836.1971, 739.9811, 786.1134, …
gapminder %>% count(country)
## # A tibble: 142 × 2
## country n
## <fct> <int>
## 1 Afghanistan 12
## 2 Albania 12
## 3 Algeria 12
## 4 Angola 12
## 5 Argentina 12
## 6 Australia 12
## 7 Austria 12
## 8 Bahrain 12
## 9 Bangladesh 12
## 10 Belgium 12
## # ℹ 132 more rows
gapminder %>% count(continent)
## # A tibble: 5 × 2
## continent n
## <fct> <int>
## 1 Africa 624
## 2 Americas 300
## 3 Asia 396
## 4 Europe 360
## 5 Oceania 24
# 대륙별 평균 기대 수량
gapminder %>% group_by(continent) %>% summarise(m=mean(lifeExp))
## # A tibble: 5 × 2
## continent m
## <fct> <dbl>
## 1 Africa 48.9
## 2 Americas 64.7
## 3 Asia 60.1
## 4 Europe 71.9
## 5 Oceania 74.3
# 오름차순, 내림차순
gapminder %>% group_by(continent) %>% summarise(m=mean(lifeExp)) %>% arrange()
## # A tibble: 5 × 2
## continent m
## <fct> <dbl>
## 1 Africa 48.9
## 2 Americas 64.7
## 3 Asia 60.1
## 4 Europe 71.9
## 5 Oceania 74.3
gapminder %>% group_by(continent) %>% summarise(m=mean(lifeExp)) %>% arrange(desc(m))
## # A tibble: 5 × 2
## continent m
## <fct> <dbl>
## 1 Oceania 74.3
## 2 Europe 71.9
## 3 Americas 64.7
## 4 Asia 60.1
## 5 Africa 48.9
tips <- read.csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/tips.csv')
glimpse(tips)
## Rows: 244
## Columns: 7
## $ total_bill <dbl> 16.99, 10.34, 21.01, 23.68, 24.59, 25.29, 8.77, 26.88, 15.0…
## $ tip <dbl> 1.01, 1.66, 3.50, 3.31, 3.61, 4.71, 2.00, 3.12, 1.96, 3.23,…
## $ sex <chr> "Female", "Male", "Male", "Male", "Female", "Male", "Male",…
## $ smoker <chr> "No", "No", "No", "No", "No", "No", "No", "No", "No", "No",…
## $ day <chr> "Sun", "Sun", "Sun", "Sun", "Sun", "Sun", "Sun", "Sun", "Su…
## $ time <chr> "Dinner", "Dinner", "Dinner", "Dinner", "Dinner", "Dinner",…
## $ size <int> 2, 3, 3, 2, 4, 4, 2, 4, 2, 2, 2, 4, 2, 4, 2, 2, 3, 3, 3, 3,…
head(tips)
## total_bill tip sex smoker day time size
## 1 16.99 1.01 Female No Sun Dinner 2
## 2 10.34 1.66 Male No Sun Dinner 3
## 3 21.01 3.50 Male No Sun Dinner 3
## 4 23.68 3.31 Male No Sun Dinner 2
## 5 24.59 3.61 Female No Sun Dinner 4
## 6 25.29 4.71 Male No Sun Dinner 4
summarise(tips)
## data frame with 0 columns and 1 row
library(dplyr)
library(ggplot2)
tips %>% ggplot(aes(size)) + geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

tips %>% ggplot(aes(total_bill, tip)) + geom_point()

tips %>% ggplot(aes(total_bill, tip)) + geom_point(aes(col=day))

tips %>% ggplot(aes(total_bill, tip)) + geom_point(aes(col=day,pch=sex), size=3)
