getwd()
## [1] "/Users/mac/bigdata"
setwd("/Users/mac/bigdata")
getwd()
## [1] "/Users/mac/bigdata"
ls()
## character(0)
rm(list=ls())
ls()
## character(0)
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
df<-read.csv("ta1.csv",fileEncoding = "euc-kr")


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
df1 %>% slice(-1)
##       연령층 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
## 7   20세이하   6월      668        5      877
## 8   20세이하   7월      639        6      847
## 9   20세이하   8월      557        7      735
## 10  20세이하   9월      609        8      808
## 11  20세이하  10월      618        8      805
## 12  20세이하  11월      532        8      724
## 13  20세이하  12월      327        6      486
## 14   21-30세  전체    26904      335    39706
## 15   21-30세   1월     2307       31     3470
## 16   21-30세   2월     1787       22     2614
## 17   21-30세   3월     1950       26     2793
## 18   21-30세   4월     2310       16     3460
## 19   21-30세   5월     2503       29     3719
## 20   21-30세   6월     2182       29     3209
## 21   21-30세   7월     2368       32     3546
## 22   21-30세   8월     2322       30     3382
## 23   21-30세   9월     2276       28     3419
## 24   21-30세  10월     2376       38     3442
## 25   21-30세  11월     2228       26     3209
## 26   21-30세  12월     2295       28     3443
## 27   31-40세  전체    28402      317    41287
## 28   31-40세   1월     2336       28     3418
## 29   31-40세   2월     2001       30     2903
## 30   31-40세   3월     1969       19     2820
## 31   31-40세   4월     2409       29     3534
## 32   31-40세   5월     2525       26     3613
## 33   31-40세   6월     2341       22     3446
## 34   31-40세   7월     2463       22     3615
## 35   31-40세   8월     2330       30     3428
## 36   31-40세   9월     2268       25     3285
## 37   31-40세  10월     2589       32     3750
## 38   31-40세  11월     2489       23     3579
## 39   31-40세  12월     2682       31     3896
## 40   41-50세  전체    33915      409    48389
## 41   41-50세   1월     2939       33     4156
## 42   41-50세   2월     2197       20     3086
## 43   41-50세   3월     2391       27     3302
## 44   41-50세   4월     2805       29     4013
## 45   41-50세   5월     3096       34     4321
## 46   41-50세   6월     2760       37     3878
## 47   41-50세   7월     2930       44     4296
## 48   41-50세   8월     2776       30     4056
## 49   41-50세   9월     2900       37     4255
## 50   41-50세  10월     3083       38     4543
## 51   41-50세  11월     2994       43     4182
## 52   41-50세  12월     3044       37     4301
## 53   51-60세  전체    44581      570    63632
## 54   51-60세   1월     3683       40     5243
## 55   51-60세   2월     2849       34     4085
## 56   51-60세   3월     3191       37     4345
## 57   51-60세   4월     3772       56     5385
## 58   51-60세   5월     3989       47     5745
## 59   51-60세   6월     3733       50     5266
## 60   51-60세   7월     3747       49     5414
## 61   51-60세   8월     3677       44     5400
## 62   51-60세   9월     3872       61     5485
## 63   51-60세  10월     4156       53     6083
## 64   51-60세  11월     4001       52     5554
## 65   51-60세  12월     3911       47     5627
## 66   61-64세  전체    19160      297    27566
## 67   61-64세   1월     1494       19     2178
## 68   61-64세   2월     1112       16     1578
## 69   61-64세   3월     1211       23     1658
## 70   61-64세   4월     1535       20     2309
## 71   61-64세   5월     1735       23     2503
## 72   61-64세   6월     1691       29     2432
## 73   61-64세   7월     1705       24     2472
## 74   61-64세   8월     1681       29     2477
## 75   61-64세   9월     1751       27     2464
## 76   61-64세  10월     1855       43     2679
## 77   61-64세  11월     1703       27     2430
## 78   61-64세  12월     1687       17     2386
## 79   65-70세  전체    19399      349    27792
## 80   65-70세   1월     1395       17     1973
## 81   65-70세   2월     1042       14     1478
## 82   65-70세   3월     1234       26     1660
## 83   65-70세   4월     1554       26     2229
## 84   65-70세   5월     1792       27     2613
## 85   65-70세   6월     1633       25     2339
## 86   65-70세   7월     1724       34     2512
## 87   65-70세   8월     1614       38     2333
## 88   65-70세   9월     1777       32     2528
## 89   65-70세  10월     1960       31     2816
## 90   65-70세  11월     1945       37     2769
## 91   65-70세  12월     1729       42     2542
## 92  71세이상  전체    15253      386    21489
## 93  71세이상   1월     1074       19     1453
## 94  71세이상   2월      845       23     1148
## 95  71세이상   3월     1018       22     1393
## 96  71세이상   4월     1296       33     1828
## 97  71세이상   5월     1453       43     2013
## 98  71세이상   6월     1257       34     1740
## 99  71세이상   7월     1324       30     1877
## 100 71세이상   8월     1306       34     1865
## 101 71세이상   9월     1483       39     2095
## 102 71세이상  10월     1647       42     2396
## 103 71세이상  11월     1443       41     1994
## 104 71세이상  12월     1107       26     1687
## 105     불명  전체     2714        0     3079
## 106     불명   1월      231        0      267
## 107     불명   2월      172        0      191
## 108     불명   3월      183        0      204
## 109     불명   4월      211        0      231
## 110     불명   5월      259        0      290
## 111     불명   6월      216        0      242
## 112     불명   7월      215        0      233
## 113     불명   8월      233        0      278
## 114     불명   9월      280        0      305
## 115     불명  10월      224        0      261
## 116     불명  11월      243        0      302
## 117     불명  12월      247        0      275
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
## function (x, width = NULL, ...) 
## {
##     UseMethod("glimpse")
## }
## <bytecode: 0x10a8ae1c8>
## <environment: namespace:pillar>
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, …
library(dplyr)
y<-gapminder %>% group_by(year, continent) %>% summarize(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(log10(gapminder$gdpPercap),gapminder$lifeExp,col=gapminder$continent)
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, …
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(year==1952&continent=="Europe") %>%
  ggplot(aes(reorder(country,pop),pop))+geom_bar(stat = 'identity')+coord_flip()

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

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

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