﻿# 예제 3-1
A_score <- c(90, 80, 75, 60, NA); A_score

# 예제 3-2
B_score <- c(90, 80, 75, 60, NULL); B_score

# 예제 3-3
str(A_score)
str(B_score)

# 예제 3-4
x <- c(TRUE, FALSE)
y <- c(T, F)
z <- TRUE
x
y
z

# 예제 3-5
class(x)
class(y)
class(z)

# 예제 3-6
x <- TRUE
y <- F
result_1 <- x == y
result_2 <- x != y
result_1
result_2

# 예제 3-7
sex <- factor(c("m", "f", "f", "m", "f")); sex

# 예제 3-8
nlevels(sex)
levels(sex)
str(sex)

# 예제 3-9
x <- c("가", "나", "다"); x
class(x)

# 예제 3-10
# integer(정수)
x <- 1:10; x
# numeric(실수)
y <- c(0.1, 0.2); y
# complex(복소수)
z <- c(1+0i, 2+4i); z
class(x)
class(y)
class(z)

# 예제 3-11
x <- "데이터"
is.integer(x)
is.numeric(x)
is.double(x)
is.complex(x)
is.character(x)
is.na(x)

# 예제 3-12
x <- c(1,2,3,4,5); x
typeof(x)

# 예제 3-13
is.integer(x)
is.character(x)
is.double(x)

# 예제 3-14
x <- c(1,2,3,4,5)
is.numeric(x)
is.double(x)
typeof(x)

# 예제 3-15
x <- c(1,2,3,4,5); x
typeof(x)

# 예제 3-16
y <- c(1,2,3,4,5, "한글"); y
typeof(y)

# 예제 3-17
x <- list(1,2,"3",4,5,6)
x[3]
x[[3]]

# 예제 3-18
typeof(x[3])
typeof(x[[3]])

# 예제 3-19
arr <- array(1:3, c(2,4,2))
arr

# 예제 3-20
is.array(arr)
dim(arr)

# 예제 3-21
attr(arr, "dim") <- NULL
arr
dim(arr)

# 예제 3-22
is.array(arr)
is.vector(arr)

# 예제 3-23
mat1 <- matrix(1:10)
mat1

# 예제 3-24
mat2 <- matrix(1:50,10,5)
mat2

# 예제 3-25
df <- data.frame(ID=c("10001", "10002","10003", "10004"), Score=c(90,21,70,80))
df

# 예제 3-26
names(df)
dim(df)
str(df)
summary(df)
nrow(df)
ncol(df)

# 예제 3-27
#1행 2열 선택
df[1,2]
#1행 1,2열 선택
df[1,c(1,2)]
#Score열 선택
df$Score
#Scroe가 60점 이상인 것 선택
df[df$Score >= 60, ]
#ID가 10001인 것 선택
df[df$ID == 10001, ]

# 예제 3-28
1 + 1
2 * 2
(1 + 2) - 1

# 예제 3-29
x <- c(1,2,3,4,5); x

# 예제 3-30
x <- c(1,2,3,4,5); x
y <- c(5,4,3,2,1); y
z <- x * y; z

# 예제 3-31
x <- c(1, 2, 3, 4, 5)
y <- c(1,2)
z <- x * y; z

# 예제 3-32
x <- c(1,2,3,4,5); x
y = c(1,2,3,4,5); y
z <<- c(1,2,3,4,5); z

c(1,2,3,4,5) -> xx ; xx
c(1,2,3,4,5) ->> zz ; zz

# 예제 3-33
1 + 1
1 - 1
1 * 1
1 / 1
#2를 3번 곱하는 거듭제곱
2 ^ 3
2 ** 3
#7을 3으로 나눈 나머지 값
7 %% 3
#7을 3으로 나눈 몫 값
7 %/% 3

# 예제 3-34
#기준 조건 비교대상 
#1하고 1이 같은가?
1 == 1

#1하고 1이 다른가?
1 != 1

#1이 2보다 큰가?
1 > 2

#1이 2보다 작은가?
1 < 2

#1이 2보다 크거나 같은가?
1 >= 2

#1이 2보다 작거나 같은가?
1 <= 2

# 예제 3-35
x <- c(1, 0, 1, 0); x
y <- c(0, 0, 1, 1); y

(x == 0) & (y == 0)
(x == 0) && (y == 0)

# 예제 3-36
x <- c(1, 0, 1, 0); x
y <- c(0, 0, 1, 1); y

(x == 0) | (y == 0)
(x == 0) || (y == 0)

# 예제 3-37
x <- c(1, 0, 1, 0); x
y <- c(0, 0, 1, 1); y

x == y
!(x == y)
x != y

(x == 0) | (y == 0)
(x == 0) || (y == 0)
!(x == 0) | (y == 0)
!(x == 0) || (y == 0)

# 예제 3-38
x <- c(TRUE, FALSE, TRUE, FALSE); x
y <- c(TRUE, FALSE, FALSE, TRUE); y
xor(x, y)

# 예제 3-39
x <- c(1, 2, 1, 0); x
y <- c(0, 0, 1, 1); y
xor(x, y)

# 예제 3-40
x <- TRUE; x
y <- c(TRUE, TRUE); y
z <- c(TRUE, FALSE, FALSE); z
isTRUE(x)
isTRUE(y)
isTRUE(z)

# 예제 3-42
myfunction1 <- function(x, y){
  z <- x * y
  return(z)
}
myfunction1(2, 4)

# 예제 3-43
myfunction2 <- function(){
  x <- 1:10
  return(x)
}
myfunction2()

# 예제 3-44
myfunction3 <- function(x, y=4){
  z <- x * y
  return(z)
}
myfunction3(2)
myfunction3(2, 3)

# 예제 3-45
myfunction4 <- function(x, y){
  result_sum <- sum(x, y)
  result_max <- max(x, y)
  return(c(result_sum, result_max))
}
myfunction4(2, 5)

# 예제 3-46
x <- c(1, 2, 3, 4, 5); x
max(x)
min(x)
sum(x)
mean(x)
median(x)
summary(x)

# 예제 3-47
y <- c(3, 5, 8, 11, 1); y
cumsum(y)
#3, 3+5=8, 8+8=16, 16+11=27, 27+1=28
cumprod(y)
#3, 3*5=15, 15*8=120, 120*11=1320, 1320
diff(y)
#5-3=2, 8-5=3, 11-8=3, 1-11=-10
order(y)
#5=다섯번째 위치에 있는 1이 가장 적으므로 5, 그 다음 3부터 순서대로 이므로 3의 위치 1
rank(y)
#각 위치의 순위이므로 3은 2위, 마지막 1은 1위
range(y)
#1부터 11까지의 수가 있으므로 1과 11

# 예제 3-48
#전역변수
x <- 1; y <- 2; z <- 3;
ft <- function(x)
{
  #지역 변수
  y <- x + 1
  #매개 변수, 지역 변수, 전역 변수
  print(x); print(y); print(z)
}

x; y; z;
ft(x)
ft(2)

# 예제 3-49
x <- 3; x
y <- 3; y
ft <- function()
{
  x <- 1 
  y <<- 1 
}
ft()
x; y

# 예제 3-51
x <- c(1, 2, 3, 4, 5)
if(any(x==1)) "x에 1이 있다"

# 예제 3-53
x <- 2
{ 
  if(x > 0){ 
    x <- x * x
  }else{
    x <- x * 0
  }
  print(x)
}

# 예제 3-55
x <- 0
{ 
  if(x > 0){ 
    x <- x * x
  }else if(x == 0){
    x <- 1
  }else{
    x <- x * 0
  }
  print(x)
}

# 예제 3-57
x <-2 
ifelse(x > 0, {x <- x * x}, {x <- x * 0})

# 예제 3-59
ft <- function(x){ 
  if(x > 0){ 
    x <- x * x
  }else if(x == 0){
    x <- 1
  }else{
    x <- x * 0
  }
  return(x)
}
ft(2)
ft(0)
ft(-1)

# 예제 3-61
ftswitch <- function(x)
{
  switch(x,
         "1" = "하나",
         "2" = "둘",
         "3" = "셋",
         "4" = "넷",
         "기타")
}
ftswitch(1)
ftswitch(3)
ftswitch(5)

# 예제 3-63
for(i in 1:10){
  print(i)
}

# 예제 3-64
x <- c("a","b","c","d","e")
for(test in x){
  print(test)
}

# 예제 3-65
ftfor <- function(x){
  for(i in 1:x){
    print(i)
  }
}
ftfor(5)

# 예제 3-66
ftfor <- function(x){ 
  i <- 0
  for(j in 1:x){
    i <- i + j
    print(c(i,j))
  }
}
ftfor(5)

# 예제 3-67
for(i in 1:10){
  if(i == 5) next
  print(i)
}

# 예제 3-68
for(i in 1:10){
  if(i == 5) break
  print(i)
}

# 예제 3-70
i <- 0
while(i < 5){
  i <- i + 1
  print(i)
}

# 예제 3-71
i <- 0
while(i < 10){
  i <- i + 1
  if(i==5) next
  print(i)
}

# 예제 3-72
i <- 0
while(i < 10){
  i <- i + 1
  if(i == 5) break
  print(i)
}

#참고
install.packages("sqldf")
library(sqldf)
sqldf('select * from iris limit 3')
sqldf('select * from iris where "Sepal.Length" > 7')


# 예제 4-1
install.packages('dplyr')
library(dplyr)
install.packages("tidyr")
library(tidyr)

# 예제 4-2
iris

# 예제 4-3
#iris data를 tbl_iris 변수에 넣자.
tbl_iris <- tbl_df(iris)

#tbl_iris data를 확인해보자.
tbl_iris

# 예제 4-4
iris$Sepal.Len
tbl_iris$Sepal.Len

# 예제 4-5
class(iris)
class(tbl_iris)

# 예제 4-6
str(mtcars)
glimpse(mtcars)

# 예제 4-7
result1 <- tbl_iris[tbl_iris$Species=="setosa",  ]
result2 <- summary(result1)
print(result2)

result1 <- tbl_iris %>% 
  filter(Species == 'setosa') %>% 
  summary() %>% 
  print()

# 예제 4-9
tbl_iris
gather(tbl_iris, Type, value, Sepal.Length:Petal.Width)

# 예제 4-11
mtcars_10 <- head(mtcars, 10) %>% print()
spread(mtcars_10, gear, mpg)

# 예제 4-13
mtcars_name <- data.frame(Names = rownames(mtcars)); mtcars_name

# 예제 4-14
result <- separate(mtcars_name, Names, c("A", "B"), sep = " ", fill='right', extra='merge'); result

# 예제 4-16
mtcars_name <- unite(result, Names, A, B, sep=' '); mtcars_name

# 예제 4-18
filter(tbl_iris, Species=='versicolor')

# 예제 4-20
slice(tbl_iris, 6:15)

# 예제 4-22
select(tbl_iris, Sepal.Length, Sepal.Width, Petal.Length, Petal.Width)

# 예제 4-23
select(tbl_iris, starts_with("S"))

# 예제 4-23
vars <- c("Sepal.Length", "Petal.Length")
select(tbl_iris, one_of(vars))

# 예제 4-24
tbl_iris2 <- tbl_iris
colnames(tbl_iris2) <- sprintf("x%d", 1:5)
tbl_iris2

select(tbl_iris2, num_range("x", 1:2))

# 예제 4-25
select_if(tbl_iris, is.double)

# 예제 4-28
tbl_iris
mutate(tbl_iris, SepalLengthAndWidth = Sepal.Length * Sepal.Width)

# 예제 4-30
transmute(tbl_iris, SepalLengthAndWidth = Sepal.Length * Sepal.Width)

# 예제 4-31
x <- data.frame(A=c('a','b','c'), B=c('t','u','v'), C=c(1,2,3))
y <- data.frame(A=c('a','b','c'), B=c('t','u','w'), D=c(3,2,1))
x
y

# 예제 4-33
bind_cols(x, y)

# 예제 4-35
left_join(x, y, by=c('A','B'))

# 예제 4-37
right_join(x, y, by=c('A','B'))

# 예제 4-39
inner_join(x, y, by=c('A','B'))

# 예제 4-41
full_join(x, y, by=c('A','B'))

# 예제 4-42
x <- data.frame(A=c('a','b','c'), B=c('t','u','v'), C=c(1,2,3), stringsAsFactors = F)
y <- data.frame(A=c('a','b','c'), B=c('t','u','w'), C=c(1,2,4), stringsAsFactors = F)
x
y

# 예제 4-43
bind_rows(x, y)

# 예제 4-45
intersect(x,y)

# 예제 4-47
setdiff(x,y)

# 예제 4-49
union(x,y)

# 예제 5-1
install.packages('XML')
library(XML)

# 예제 5-2
url <- 'http://openapi.seoul.go.kr:8088/(인증키)/xml/SebcPublicEnterpriseKor/1/1000/'
indata <- xmlToDataFrame(url)
View(indata)

# 예제 5-3
indata <- indata[-1:-2,-1:-3] 

# 예제 5-4
install.packages("jsonlite")
library(jsonlite)

# 예제 5-5
data2 <- fromJSON("https://api.github.com/users/hadley/orgs")
data2 <- as.data.frame(data2)
colnames(data2)
View(data2)

# 예제 5-6
data <- fromJSON("C:/r_temp/전국초등학교통학구역표준데이터.json")
names(data)
data <- as.data.frame(data$records)
View(data)

# 예제 5-7
install.packages(c("DBI","RODBC"))
library(DBI)
library(RODBC)

# 예제 5-8
con <- odbcConnect('test', uid='test', pwd='test1!')
data <- sqlQuery(con,"select * from table.name")
data
odbcCloseAll()

# 예제 5-9
install.packages('RMySQL')
library(RMySQL)

# 예제 5-10
data <- dbConnect(MySQL(), user="test", password="test!1", host="000.000.000.000", dbname="data", client.flag=CLIENT_MULTI_RESULTS)
head(dbGetQuery(data, "SELECT * FROM table.name"))
dbDisconnect(data)

# 예제 5-11
install.packages(c("readr","DBI","pool","dplyr","dbplyr","RSQLServer"))
library(readr)
library(DBI)
library(pool)
library(dplyr)
library(dbplyr)
library(RSQLServer)

# 예제 5-12
NHIS_OPEN_GJ_2015 <- read_csv("C:/r_temp/NHIS_OPEN_GJ_2015.CSV", locale = locale(encoding = "euc-kr"))
data <- NHIS_OPEN_GJ_2015[1:100, 1:7]
colnames(data) <- c("Year", "UID", "Sex", "Age", "CityCode", "height", "Weight")

# 예제 5-13
con <- dbPool(RSQLServer::SQLServer(), server="test.database.windows.net", database="test_server", properties=list(user=" 0000 ", password="0000000"))
dbListTables(con)
dbWriteTable(con, "NHIS_OPEN_GJ_2015", data, overwrite=TRUE, row.names = TRUE)
dbListTables(con)
dbReadTable(con, "NHIS_OPEN_GJ_2015")

# 예제 5-14
result <- tbl(con, "NHIS_OPEN_GJ_2015") %>% filter(Weight >= 80)
result
poolClose(con)

# 예제 5-15
library(readr)
library(DBI)
library(dplyr)
library(dbplyr)
library(RSQLServer)

# 예제 5-16
con <- dbConnect(RSQLServer::SQLServer(), server="test.database.windows.net", database="test_server", properties=list(user="0000", password="00000000"))
dbListTables(con)
dbReadTable(con, "NHIS_OPEN_GJ_2015")

# 예제 5-17
result <- tbl(con, "NHIS_OPEN_GJ_2015") %>% filter(Weight >= 80)
result
dbDisconnect(con)

# 예제 5-18
con <- dbPool(RSQLServer::SQLServer(), server="test7.database.windows.net", database="test_server", properties=list(user="id", password="pass"))
con <- dbConnect(RSQLServer::SQLServer(), server="test7.database.windows.net", database="test_server", properties=list(user="id", password="pass"))
poolClose(con)
dbDisconnect(con)

# 예제 5-19
data <- data.frame(number=rnorm(n=100000000))
dim(data)

# 예제 5-20
#RData 파일 형식 쓰기
system.time({
  save(data, file="data.RData")
})
#csv 파일 형식 쓰기
system.time({
  write_csv(data, "data.csv")
})
#feather 파일 형식 쓰기
system.time({
  write_feather(data, "data.feather")
})
#fst 파일 형식 쓰기
system.time({
  write.fst(data, "data.fst")
})

# 예제 5-21
#RData 파일 형식 읽기
system.time({
  load("data.RData")
})
#csv 파일 형식 읽기
system.time({
  data1 <- read_csv("data.csv")
})
#feather 파일 형식 읽기
system.time({
  data2 <- read_feather("data.feather")
})
#fst 파일 형식 읽기
system.time({
  data3 <- read.fst("data.fst")
})

# 예제 6-1
NHIS_OPEN_GJ_2015 <- read_csv("C:/r_temp/test/NHIS_OPEN_GJ_2015.CSV", locale = locale(encoding = "euc-kr"))

# 예제 6-2
summary(NHIS_OPEN_GJ_2015)

# 예제 6-3
summary(as.factor(NHIS_OPEN_GJ_2015$성별코드))

# 예제 6-4
library(XML)
library(dplyr)

# 예제 6-5
url <- 'http://openapi.seoul.go.kr:8088/(인증키)/xml/SebcPublicEnterpriseKor/1/1000/'
indata <- xmlToDataFrame(url)
indata <- indata[-1:-2,-1:-3]
View(indata)

# 예제 6-6
result_1 <- indata %>% 
  select(H_KOR_CITY) %>% 
  group_by(H_KOR_CITY) %>% 
  count() %>% 
  print()

# 예제 6-7
result_1 <- indata %>% 
  #result_1에다 최종 결과값을 넣는다. indata를 사용한다.
  select(H_KOR_CITY) %>%
  #indata에서 H_KOR_CITY컬럼만 선택한다.
  group_by(H_KOR_CITY) %>% 
  #indata에서 H_KOR_CITY컬럼 기준으로 그룹화한다.
  count() %>% 
  #H_KOR_CITY컬럼기준으로 몇개인지 카운트한다.
  print()
  #결과를 출력한다.

# 예제 6-8
mean(result_1$n)

# 예제 6-9
result_1 <- indata %>% 
  select(H_KOR_CITY) %>% 
  group_by(H_KOR_CITY) %>% 
  count() %>%
  ungroup() %>% 
  summarise(Mean=mean(n),Mean2=sum(n)/n(),Sum=sum(n),Count=n()) %>% 
  print()

# 예제 6-10
result_1 <- indata %>% 
  select(H_KOR_CITY) %>% 
  group_by(H_KOR_CITY) %>% 
  count() %>%
  ungroup() %>% 
  summarise(SD=sd(n)) %>% 
  print()

# 예제 6-11
result_1 <- indata %>% 
  select(H_KOR_CITY) %>% 
  group_by(H_KOR_CITY) %>% 
  count() %>%
  ungroup() %>% 
  summary() %>% 
  print()

# 예제 6-12
library(XML)
library(dplyr)
url <- 'http://openapi.seoul.go.kr:8088/6e4748666e697a6f313236416d4c5775/xml/SebcPublicEnterpriseKor/1/1000/'
indata <- xmlToDataFrame(url)
indata <- indata[-1:-2,-1:-3]
result_1 <- round(prop.table(table(indata$H_KOR_CITY))*100,1)
result_1 <- as.data.frame(result_1)
result_1

# 예제 6-13
result_2 <- indata %>% 
  select(H_KOR_CITY) %>% 
  group_by(H_KOR_CITY) %>% 
  summarise(Count=n()) %>%
  mutate(TotalCount=sum(Count)) %>% 
  mutate(Percent=round((Count/TotalCount)*100,1)) %>% 
  mutate(CumTotal=cumsum(Count), CumPer=cumsum(Percent)) %>% 
  select(H_KOR_CITY, Count, CumTotal, TotalCount, Percent, CumPer)
result_2

# 예제 6-14
round(0.5,0)
round(0.4,0)
round(0.5,0)
round(0.6,0)

# 예제 6-15
rou <- function(x, n)
{
  d=10^n
  x=x*d
  r=trunc(x+0.5)
  x=r
  x=x/d
  return(x)
}

# 예제 6-16
rou(0.4,0)
rou(0.5,0)
rou(0.6,0)

# 예제 6-17
x <- 0:50
quantile(x)
quantile(x, c(0.05, 0.1, 0.9, 0.95))

# 예제 6-18
NHIS_OPEN_GJ_2015 <- read_csv("C:/r_temp/NHIS_OPEN_GJ_2015.CSV", locale = locale(encoding = "euc-kr"))

# 예제 6-19
glimpse(NHIS_OPEN_GJ_2015)

# 예제 6-20
quantile(NHIS_OPEN_GJ_2015$`신장(5Cm단위)`)
quantile(NHIS_OPEN_GJ_2015$`체중(5Kg 단위)`)

# 예제 6-21
quantile(NHIS_OPEN_GJ_2015$`신장(5Cm단위)`,c(0.9, 0.99))
quantile(NHIS_OPEN_GJ_2015$`체중(5Kg 단위)`,c(0.9, 0.99))

# 예제 6-22
x <- data.frame(x=rnorm(n=5000, mean=0, sd=1))
ggplot(data=x, aes(x)) + geom_histogram()

# 예제 6-23
shapiro.test(x$x)

# 예제 6-24
data <- NHIS_OPEN_GJ_2015 %>% 
  select(성별코드, 흡연상태)

table(data)

# 예제 6-25
summary(table(data))

# 예제 6-26
chisq.test(data$성별코드, data$흡연상태)

# 예제 6-27
data <- NHIS_OPEN_GJ_2015 %>% select(성별코드, `신장(5Cm단위)`)
head(data)

# 예제 6-28
table(data)

# 예제 6-29
t.test(data$성별코드, data$`신장(5Cm단위)`)

# 예제 6-30
data <- NHIS_OPEN_GJ_2015 %>% select(흡연상태, `체중(5Kg 단위)`)
head(data)

# 예제 6-31
table(data)

# 예제 6-32
data <- data %>% 
  mutate(흡연상태=as.factor(흡연상태))
result_1 <- lm(`체중(5Kg 단위)`~흡연상태, data=data)
result_2 <- aov(`체중(5Kg 단위)`~흡연상태, data=data)
anova(result_1)
anova(result_2)

# 예제 7-1
data <- NHIS_OPEN_GJ_2015 %>% 
  select(`신장(5Cm단위)`, `체중(5Kg 단위)`)
head(data)

# 예제 7-4
cor(data$`신장(5Cm단위)`, data$`체중(5Kg 단위)`,  method="pearson")

# 예제 7-5
cor(data, method="pearson")

# 예제 7-6
cor.test(data$`신장(5Cm단위)`, data$`체중(5Kg 단위)`)

# 예제 7-7
data <- NHIS_OPEN_GJ_2015 %>% select(`체중(5Kg 단위)`, 허리둘레, `신장(5Cm단위)`)

# 예제 7-8
set.seed(123)
idx <- sample(2, size=NROW(data), replace=T, prob=c(0.7, 0.3))
train_data <- data[idx==1, ]
test_data <- data[idx==2, ]

# 예제 7-9
train_result <- lm(`체중(5Kg 단위)`~허리둘레+`신장(5Cm단위)`, data=train_data)
summary(train_result)

# 예제 7-10
test_result <- predict(train_result, newdata=test_data)
test_result <- round(test_result, 0)
test_result <- test_result %>% 
  as.data.frame() %>% 
  mutate(raw_value=test_data$`체중(5Kg 단위)`)
names(test_result) <- c('result_value', 'raw_value')
result <- test_result %>% 
  mutate(Correct=ifelse(result_value==raw_value, 1, 0)) %>% 
  group_by(Correct) %>% 
  summarise(Count=n()) %>% 
  print()

# 예제 7-11
head(iris)

# 예제 7-12
set.seed(123)
idx <- sample(2, size=NROW(iris), replace=T, prob=c(0.7, 0.3))
train_data <- iris[idx==1, ]
test_data <- iris[idx==2, ]

# 예제 7-13
install.packages("party")
library(party)
iris_tree <- ctree(Species ~ ., data=train_data); iris_tree

# 예제 7-14
plot(iris_tree)
plot(iris_tree, type="simple")

# 예제 7-15
table(train_data$Species)
table(predict(iris_tree), train_data$Species)

# 예제 7-16
test_result <- predict(iris_tree, newdata=test_data)
table(test_data$Species)
table(test_result, test_data$Species)

# 예제 7-17
library(caret)
confusionMatrix(test_result, test_data$Species)

# 예제 7-18
set.seed(123)
idx <- sample(2, size=NROW(iris), replace=T, prob=c(0.7, 0.3))
train_data <- iris[idx==1, ]
test_data <- iris[idx==2, ]

# 예제 7-19
install.packages("randomForest")
library(randomForest)
iris_forest <- randomForest(Species ~ ., data=train_data, importance = T)
iris_forest

# 예제 7-20
table(train_data$Species)
table(iris_forest$predicted, train_data$Species)

# 예제 7-21
iris_forest$importance

# 예제 7-22
varImpPlot(iris_forest)

# 예제 7-23
test_result <- predict(iris_forest, newdata=test_data)
table(test_data$Species)
table(test_result, test_data$Species)

# 예제 7-24
library(caret)
confusionMatrix(test_result, test_data$Species)

# 예제 8-1
install.packages('ggplot2')
library(ggplot2)

# 예제 8-2
vignette("ggplot2-specs")
vignette("extending-ggplot2")

# 예제 8-3
ggplot(data, aes(x=length, y=width)) +
  geom_point(colour='blue') + 
  geom_smooth()

# 예제 8-4
ggplot(data=cars, aes(x=speed, y=dist))

# 예제 8-5
x <- ggplot(data=cars, aes(x=speed, y=dist))
summary(x)

# 예제 8-6
ggplot(data=cars, aes(x=speed, y=dist)) + 
  geom_point(colour="red")

# 예제 8-7
ggplot(mpg, aes(displ, hwy, colour = class)) +
  geom_point() +
  geom_smooth(se = FALSE, method = lm)

# 예제 8-8
ggplot(mpg, aes(displ, hwy, colour = class)) +
  geom_point(colour = 'red') +
  geom_smooth(se = FALSE, method = lm) 

# 예제 8-9
p <- ggplot(data=diamonds, aes(x=carat, y=price))
p + geom_smooth()

# 예제 8-10
p <- ggplot(data=diamonds, aes(x=carat,y=price))
p + geom_smooth(aes(group=cut))

# 예제 8-11
p <- ggplot(data=diamonds, aes(x=carat,y=price))
p + geom_smooth(aes(colour=cut))

# 예제 8-12
ggplot(data=diamonds, aes(x=price)) + geom_histogram() #-– (1)
ggplot(data=diamonds, aes(x=price)) + geom_histogram(aes(y=..count..)) #-– (2)

# 예제 8-13
ggplot(data=diamonds, aes(x=price)) + geom_histogram(aes(y=..ncount..))

# 예제 8-14
x <- ggplot(diamonds, aes(price))
x + stat_bin(geom="bar") #-- (1)
x + stat_bin(geom="line") #-- (2)
x + stat_bin(aes(size=..ncount..), geom="point") #-- (3)

# 예제 8-15
result <- ggplot(iris, aes(Sepal.Length)) + geom_dotplot()
result

# 예제 8-16
summary(iris$Sepal.Length)
range(iris$Sepal.Length)

# 예제 8-17
result <- ggplot(iris, aes(Sepal.Length)) + geom_dotplot(binwidth = 3.6/ 30)
result

# 예제 8-18
result <- ggplot(iris, aes(Sepal.Length)) + geom_area(stat='count')
result

# 예제 8-19
table(iris$Sepal.Length)

# 예제 8-20
result <- ggplot(iris, aes(Sepal.Length)) + geom_bar()
result

# 예제 8-21
result <- ggplot(iris,aes(Sepal.Length)) + geom_bar() + coord_flip()
result

# 예제 8-22
result <- ggplot(iris, aes(Sepal.Length)) + geom_bar() + coord_flip() + scale_x_reverse()
result

# 예제 8-23
result <- ggplot(iris, aes(Sepal.Length)) + geom_bar(fill="deepskyblue4", alpha=0.3)
result

# 예제 8-24
result <- ggplot(iris,aes(Sepal.Length)) + geom_histogram(binwidth =0.5, fill="deepskyblue4", colour='black', alpha=0.3)
result

# 예제 8-25
View(pressure)

# 예제 8-26
result <- ggplot(pressure,aes(temperature, pressure)) + geom_line()
result

# 예제 8-27
result <- ggplot(pressure, aes(temperature, pressure)) + geom_area()
result

# 예제 8-28
result <- ggplot(pressure,aes(temperature, pressure)) + geom_step()
result

# 예제 8-29
result <- ggplot(iris,aes(Sepal.Length,Sepal.Width)) + geom_point()
result

# 예제 8-30
install.packages('ggThemeAssist')
library(ggplot2)
library(ggThemeAssist)

# 예제 8-31
gg <- ggplot(mtcars, aes(x = hp, y = mpg, colour = as.factor(cyl))) + geom_point() + theme()
gg

# 예제 8-32
ggThemeAssistGadget(gg)

# 예제 8-33
gg + theme(plot.subtitle = element_text(vjust = 1), plot.caption = element_text(vjust = 1))

gg <- gg + theme(plot.subtitle = element_text(vjust  =  1))
gg <- gg + theme(plot.caption = element_text(vjust  =  1))

# 예제 8-34
gg + theme(plot.subtitle = element_text(vjust = 1), 
           plot.caption = element_text(vjust = 1), 
           plot.background = element_rect(fill = "azure4", 
                                          colour = "yellow", size = 2, linetype = "dotdash"))

# 예제 8-35
gg + theme(plot.subtitle = element_text(vjust = 1), 
           plot.caption = element_text(vjust = 1), 
           panel.background = element_rect(fill = "cadetblue2", 
                                           colour = "black", size = 2, linetype = "dotted"))

# 예제 8-36
gg + theme(plot.subtitle = element_text(vjust = 1), 
           plot.caption = element_text(vjust = 1), 
           panel.grid.major = element_line(colour = "red", size = 1))

# 예제 8-37
gg + theme(plot.subtitle = element_text(vjust = 1), 
           plot.caption = element_text(vjust = 1), 
           panel.grid.minor = element_line(colour = "green4", size = 2, linetype = "longdash"))

# 예제 8-38
gg + theme(plot.subtitle = element_text(vjust = 1), 
           plot.caption = element_text(vjust = 1), 
           axis.text = element_text(size = 15, face = "bold", colour = "blue", vjust = -0.25, angle = 15))

# 예제 8-39
gg + theme(plot.subtitle = element_text(vjust = 1), 
           plot.caption = element_text(vjust = 1), 
           axis.text.x = element_text(family = "sans", size = 20, colour = "blue3", vjust = 0, angle = 15), 
           axis.text.y = element_text(family = "serif", size = 30, colour = "chocolate", vjust = 0, angle = -15))

# 예제 8-40
gg + theme(plot.subtitle = element_text(vjust = 1), 
           plot.caption = element_text(vjust = 1), 
           axis.line = element_line(colour = "blue", 
                                    linetype = "solid"), axis.ticks = element_line(colour = "red", size = 5))

# 예제 8-41
gg + theme(plot.subtitle = element_text(vjust = 1), 
           plot.caption = element_text(vjust = 1), 
           axis.title = element_text(size = 20, colour = "red", angle = -5), 
           plot.title = element_text(family = "serif", size = 30, face = "bold", colour = "blue", hjust = 0.5, angle = 5)) +
  labs(title = "Title", x = "X name", y = "Y name", colour = "Label")

# 예제 8-42
gg + theme(plot.subtitle = element_text(vjust = 1), 
           plot.caption = element_text(vjust = 1), 
           legend.text = element_text(size = 20, face = "italic", family = "serif", colour = "red"), 
           legend.title = element_text(size = 30, face = "bold", family = "mono", colour = "blue"), 
           legend.key = element_rect(fill = "white", colour = "red", size = 1, linetype = "dotted"), 
           legend.background = element_rect(fill = "gray", colour = "black", size = 1, linetype = "solid"), 
           legend.position = "left")

# 예제 8-43
gg + theme(plot.subtitle = element_text(family = "mono", size = 20, face = "bold", colour = "blue", vjust = 1), 
           plot.caption = element_text(size = 20, colour = "red", vjust = 1), 
           plot.title = element_text(size = 20, hjust = 0.5)) + 
  labs(title = "Title", subtitle = "Subtitle", caption = "Caption")

# 예제 8-44
gg + theme(plot.subtitle = element_text(size = 20, colour = "black", vjust = 1), 
           plot.caption = element_text(size = 20, colour = "black", vjust = 1), 
           axis.line = element_line(colour = "blue", linetype = "solid"), 
           axis.ticks = element_line(colour = "red", size = 2), 
           panel.grid.major = element_line(colour = "red"), 
           panel.grid.minor = element_line(colour = "black", linetype = "dotted"), 
           axis.title = element_text(family = "mono", size = 20, vjust = 1), 
           axis.text = element_text(size = 20), 
           plot.title = element_text(size = 20, face = "bold", hjust = 0.5, vjust = 1), 
           legend.text = element_text(size = 20), 
           legend.title = element_text(size = 20), 
           panel.background = element_rect(fill = "white", colour = "blue", size = 1, linetype = "dashed"), 
           plot.background = element_rect(fill = "gray", colour = "black", size = 2, linetype = "solid"), 
           legend.key = element_rect(fill = "gray", colour = "black", size = 1, linetype = "solid"), 
           legend.background = element_rect(colour = "black", size = 1, linetype = "solid")) +
  labs(title = "plot.title", x = "Axis.x", y = "Axis.y", colour = "legend.text", 
       subtitle = "plot.subtitle", caption = "plot.caption")

# 예제 8-45
result <- ggplot(iris,aes(Sepal.Length,Sepal.Width)) + geom_point()
ggThemeAssistGadget(result)

# 예제 8-46
result + theme(plot.subtitle = element_text(vjust = 1), 
               plot.caption = element_text(vjust = 1), 
               plot.title = element_text(size = 20, colour = "deepskyblue4", hjust = 0.5)) +
  labs(title = "iris")

# 예제 10-1
---
  title: "decision_tree"
author: "junggi"
date: "2018년 1월 27일"
output: html_document
---
  
  ```{r setup, include=FALSE}
# install.packages("party")
library(party)
```

```{r }
idx <- sample(2, size=NROW(iris), replace=T, prob=c(0.7, 0.3))
train_data <- iris[idx==1, ]
test_data <- iris[idx==2, ]

iris_tree <- ctree(Species ~ ., data=train_data)
iris_tree
```

```{r, echo=FALSE}
plot(iris_tree, main="Descision_tree_plot")
plot(iris_tree, type="simple", main="Descision_tree_simple_plot")
```
# 예제 10-2
---
  title: "decision_tree"
author: "junggi"
date: "2018년 1월 27일"
output: html_document
---
  
  ```{r setup, include=FALSE}
# install.packages("party")
library(party)
```

```{r, echo=FALSE}
idx <- sample(2, size=NROW(iris), replace=T, prob=c(0.7, 0.3))
train_data <- iris[idx==1, ]
test_data <- iris[idx==2, ]

iris_tree <- ctree(Species ~ ., data=train_data)
iris_tree

plot(iris_tree, main="Descision_tree_plot")
plot(iris_tree, type="simple", main="Descision_tree_simple_plot")
```
# 예제 10-3
> block quote

# Header 1
## Header 2
### Header 3
#### Header 4
##### Header 5
###### Header 6

***
  
  * ordered list
+ sub-item 1
+ 스페이스 4번
- sub-sub-item 1
- 스페이스 8번

***
  ___

*기울임* and _기울임_

**굵굶게** and __굶굵게__

위첨자^2^
  
  ~~취소선~~
  
  [링크](www.rstudio.com) and <http://www.rstudio.com>
  
  ___

: 표 이름

| 오른쪽정렬 | 왼쪽정렬 | 기본정렬 | 가운데정렬 |
  |------:|:-----|---------|:------:|
  | 12 | 12 | 12 | 12 |
  | 123 | 123 | 123 | 123 |
  | 1 | 1 | 1 | 1 |
  
# 예제 10-4
  ---
  title: "test"
author: "junggi"
date: "2018년 2월 4일"
output: html_document
---
  
  ```{r }
summary(cars)
```
---
  ```{r, echo=FALSE}
summary(cars)
```
---
  ```{r, echo=TRUE}
summary(cars)
```
---
  ```{r, include=FALSE}
x <- 1+1
```
---
  ```{r, eval=FALSE, include=FALSE}
x
```
---
  ```{r eval=TRUE}
x
```
---
  ```{r prompt=TRUE}
x
```
---
  
# 예제 10-5
title: "test"
author: "junggi"
date: "2018년 2월 4일"
output: html_document
---
  
  ```{r label="data"}
x <- 1:10;x
```
---
  ```{r }
```{r data}
```
---
  ```{r }
```{r data, comment=NA}
```

# 예제 10-6
---
  title: "test"
author: "junggi"
date: "2018년 2월 4일"
output: html_document
---
  ```{r setup, include=FALSE}
library(formatR)
```
---
  ```{r }
x <- 1
{ 
  if(x > 0){ 
    x <- x * x
  }else if(x == 0){
    x <- 1
  }else{
    x <- x * 0
  }
  print(x)
}
```
---
  ```{r, tidy=TRUE}
x <- 1
{ 
  if(x > 0){ 
    x <- x * x
  }else if(x == 0){
    x <- 1
  }else{
    x <- x * 0
  }
  print(x)
}
```

# 예제 10-7
library(shiny)
library(randomForest)

set.seed(123)
idx <- sample(2, size=NROW(iris), replace=T, prob=c(0.7, 0.3))
train_data <- iris[idx==1, ]
test_data <- iris[idx==2, ]

ui <- fluidPage(
  
  titlePanel("Random Forest Analysis"),
  
  sidebarLayout(
    sidebarPanel(
      sliderInput("input_ntree",
                  "Number of ntree:",
                  min = 400,
                  max = 600,
                  value = 500,
                  step = 100),
      
      sliderInput("input_mtry",
                  "Number of mtry:",
                  min = 2,
                  max = 4,
                  value = 3,
                  step = 1)
    ),
    
    mainPanel(
      verbatimTextOutput("result_print")
    )
  )
)

server <- function(input, output) {
  
  output$result_print <- renderPrint({
    
    iris_forest <- randomForest(Species ~ ., data=train_data, ntree=input$input_ntree, mtry=input$input_mtry)
    iris_forest
    
  })
}

shinyApp(ui = ui, server = server)

# 예제 10-8
library(shiny)
library(party)

set.seed(123)
idx <- sample(2, size=NROW(iris), replace=T, prob=c(0.7, 0.3))
train_data <- iris[idx==1, ]
test_data <- iris[idx==2, ]
iris_tree <- ctree(Species ~ ., data=train_data)


ui <- fluidPage(
  
  titlePanel("Decision Tree Analysis"),
  
  sidebarLayout(
    
    sidebarPanel(
      
      radioButtons("dist", "Plot type:",
                   c("Simple" = "simple",
                     "Extended" = "extended"))
      
    ),
    
    mainPanel(
      
      tabsetPanel(type = "tabs",
                  tabPanel("Plot", plotOutput("plot")),
                  tabPanel("Result", verbatimTextOutput("result_print"))
      )
      
    )
  )
)

server <- function(input, output) {
  
  output$plot <- renderPlot({
    
    plot(iris_tree, type=input$dist)
    
  })
  
  output$result_print <- renderPrint({
    
    iris_tree
    
  })
  
}

shinyApp(ui, server)

# 예제 10-9
install.packages('plotly')
library(plotly)

# 예제 10-10
x <- rnorm(100)
plot_ly (x = x, type = 'histogram')

# 예제 10-11
plot_ly(midwest, x = ~percollege, color = ~state, type = "box")

# 예제 10-12
library(shiny)
library(ggplot2)
library(plotly)

ui <- fluidPage(
  
  titlePanel("shiny+ggplot2+plotly"),
  
  sidebarLayout(
    sidebarPanel(
      radioButtons("point_colour", "Point_colour:",
                   c("Red" = "red",
                     "Blue" = "blue",
                     "White" = "white",
                     "Black" = "black")),
      radioButtons("smooth_colour", "Smooth_colour:",
                   c("Red" = "red",
                     "Blue" = "blue",
                     "White" = "white",
                     "Black" = "black"))
    ),
    
    mainPanel(
      plotlyOutput("trendPlot")
    )
  )
)

server <- function(input, output) {
  
  output$trendPlot <- renderPlotly({
    
    p <- ggplot(data = cars, aes(x = speed, y = dist)) + 
      geom_point(colour = input$point_colour) + 
      geom_smooth(colour = input$smooth_colour)
    
    ggplotly(p)
    
  })
}
shinyApp(ui = ui, server = server)


             