Sign In

[R] 조건문

Y
Yerim
  1. R

if-else 문

조건문(conditional statement): 조건에 따라 실행할 명령문
if (비교 조건){
    조건이 참일 떄 실행할 명령문(들)
} else {
    조건이 거짓일 때 실행할 명령문(들)
}
{}는 코드블록(code block)이라고 부르며, 여러 명령문을 하나로 묶어주는 역할
ifelse문
조건에 따라 선택할 값이 각각 하나씩이면 ifelse문을 사용하는 것이 편리
ifelse(비교 조건, 조건이 참일 때 선택할 값, 조건이 거짓일 때 선택할 값
if-else의 반복
# LAB.
library(svDialogs)

# 구매액 입력 받기
purchase <- dlgInput('Enter the purchase amount')$res
purchase <- as.numeric(purchase)

type <- NULL
ratio <- NULL

# 조건문 수정
if (purchase >= 300000) {
  type <- '플래티넘'
  ratio <- 0.07
} else if (purchase >= 200000) {
  type <- '골드'
  ratio <- 0.05
} else if (purchase >= 100000) {
  type <- '실버'
  ratio <- 0.03
} else {
  type <- '프렌즈'
  ratio <- 0.01
}

# 결과 출력
cat('고객님은', type, '회원으로 구매액의', ratio * 100, '%가 적립됩니다.\n')

which()

조건에 맞는 데이터의 위치 찾기
데이터 분석을 하다 보면 자신이 원하는 데이터가 벡터나 매트릭스, 데이터프레임 안에서 어디에 위치하는지 알아야 할 때가 있음
편리하게 사용할 수 있는 함수가 which(), which.max(), which.min() 함수
# 코드 7-18
score <- c(76, 84, 59, 50, 95, 60, 82, 71, 88, 84)
which(score == 69)
which(score >= 85)
max(score)
which.max(score)
min(score)
which.min(score)
which(): 조건에 맞는 갑싱 벡터 안에서 몇 번째에 위치하고 있는지 알아내는 함수
which.max(): 벡터 안에서 최댓값의 인덱스
which.min(): 벡터 안에서 최솟값의 인덱스
# 코드 7-19
score <- c(76, 84, 69, 50, 95, 60, 82, 71, 88, 84)
idx <- which(score <= 60)
score[idx] <- 61
score

idx <- which(score >= 80)
score.high <- score[idx]
score.high
# LAB.
install.packages('Stat2Data')
library(Stat2Data)
data(ChildSpeaks)
str(ChildSpeaks)

# 말문이 트인 시기는 age에 저장
idx <- which(ChildSpeaks$Age < 9)
ChildSpeaks[idx, 'm1'] <- 5
idx <- which(ChildSpeaks$Age >= 9 & ChildSpeaks$Age < 15)
ChildSpeaks[idx, 'm1'] <- 4
idx <- which(ChildSpeaks$Age >= 15 & ChildSpeaks$Age < 21)
ChildSpeaks[idx, 'm1'] <- 3
idx <- which(ChildSpeaks$Age >= 21 & ChildSpeaks$Age < 27)
ChildSpeaks[idx, 'm1'] <- 2
idx <- which(ChildSpeaks$Age >= 27)
ChildSpeaks[idx, 'm1'] <- 1
ChildSpeaks$m1

# 언어 이해력은 Gesell에 저장
idx <- which(ChildSpeaks$Gesell < 70)
ChildSpeaks[idx, 'm2'] <- 1
idx <- which(ChildSpeaks$Gesell >= 70 & ChildSpeaks$Gesell < 90)
ChildSpeaks[idx, 'm2'] <- 2
idx <- which(ChildSpeaks$Gesell >= 90 & ChildSpeaks$Gesell < 110)
ChildSpeaks[idx, 'm2'] <- 3
idx <- which(ChildSpeaks$Gesell >= 110 & ChildSpeaks$Gesell < 130)
ChildSpeaks[idx, 'm2'] <- 4
idx <- which(ChildSpeaks$Gesell >= 130)
ChildSpeaks[idx, 'm2'] <- 5

ChildSpeaks$total <- ChildSpeaks$m1 + ChildSpeaks$m2

idx <- which(ChildSpeaks$total < 3)
ChildSpeaks[idx, 'result'] <- '매우 느림'
idx <- which(ChildSpeaks$total >= 3 & ChildSpeaks$total < 5)
ChildSpeaks[idx, 'result'] <- '늦음'
idx <- which(ChildSpeaks$total >= 5 & ChildSpeaks$total < 7)
ChildSpeaks[idx, 'result'] <- '보통'
idx <- which(ChildSpeaks$total >= 7 & ChildSpeaks$total < 9)
ChildSpeaks[idx, 'result'] <- '빠름'
idx <- which(ChildSpeaks$total >= 9)
ChildSpeaks[idx, 'result'] <- '매우 빠름'

ChildSpeaks
ChildSpeaks[which.min(ChildSpeaks$total), ]
Yerim-DevNote
Subscribe to 'Yerim-DevNote'
Subscribe to my site to be the first to receive notifications and emails about the latest updates, including new posts.
Join Slashpage and subscribe to 'Yerim-DevNote'!
Subscribe
👍