getwd()
setwd('d:/data')
getwd()

ls()
rm(list=ls())
ls()

library(dplyr)
df <-read.csv('ta1.csv',fileEncoding = 'euc-kr')
View(df)
names(df)
df %>% rename(month=월별.1.,사고건수=X2022,사망자수=X2022.1,
              연령층=가해자연령층별.1.,부상자수=X2022.2)

df1<-df %>% rename(month=월별.1.,사고건수=X2022,사망자수=X2022.1,
                   연령층=가해자연령층별.1.,부상자수=X2022.2)

head(df1)
df1 %>% slice(-1)
df2<-df1 %>% slice(-1)
head(df2)
View(df2)

table(df2$month)
df2 %>% filter(month!='전체')

df2 %>% filter(month!='전체') %>% glimpse()
df2$사고건수 <-as.numeric(df2$사고건수)
glimpse(df1)

#install.packages('gapminder')
library(gapminder)
glimpse(gapminder)

#파생변수 생성
getwd()
library(dplyr)
data(cars)

#library(gapminder)
#library(dplyr)
y<-gapminder %>% group_by(year, continent) %>% summarize(c_pop=sum(pop))
head(y,20)
plot(y$year, y$c_pop)
plot(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))))
glimpse(gapminder)
#ggplot
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)) +
  geom_point(alpha=0.5) + scale_x_log10()

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

ggplot(gapminder, aes(x=gdpPercap, y=lifeExp, col=continent)) +
  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=="Africa") %>% 
  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()

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.