getwd()
## [1] "C:/data"
setwd("c:/data")
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)
names(df)
## [1] "가해자연령층별.1." "월별.1." "X2022"
## [4] "X2022.1" "X2022.2"
df1 <- df %>% rename(month=월별.1.,사고건수=X2022,사망자수=X2022.1,
연령층=가해자연령층별.1.,부상자수=X2022.2)
View(df1)
df2 <- df1 %>% slice(-1)
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)
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
View(gapminder)
plot(y$year, y$c_pop)

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(y$continent))))
#install.packages("ggplot2")
library(ggplot2)

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

scale_x_log10()
## <ScaleContinuousPosition>
## Range:
## Limits: 0 -- 1
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 %>% 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 %>% filter(country=='Korea, Rep.')
## # A tibble: 12 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Korea, Rep. Asia 1952 47.5 20947571 1031.
## 2 Korea, Rep. Asia 1957 52.7 22611552 1488.
## 3 Korea, Rep. Asia 1962 55.3 26420307 1536.
## 4 Korea, Rep. Asia 1967 57.7 30131000 2029.
## 5 Korea, Rep. Asia 1972 62.6 33505000 3031.
## 6 Korea, Rep. Asia 1977 64.8 36436000 4657.
## 7 Korea, Rep. Asia 1982 67.1 39326000 5623.
## 8 Korea, Rep. Asia 1987 69.8 41622000 8533.
## 9 Korea, Rep. Asia 1992 72.2 43805450 12104.
## 10 Korea, Rep. Asia 1997 74.6 46173816 15994.
## 11 Korea, Rep. Asia 2002 77.0 47969150 19234.
## 12 Korea, Rep. Asia 2007 78.6 49044790 23348.
#gapminder %>% filter(country=='Korea, Rep.') %>% ggplot(aes(year,lifeExp,
# ))