조건문

중급 R

Filip Schouwenaars

DataCamp Instructor

if 문

if(condition) {
  expr
}
x <- -3
if(x < 0) {
  print("x is a negative number")
}
"x is a negative number"
중급 R

if 문

if(condition) {
  expr
}
x <- 5
if(x < 0) {
  print("x is a negative number")
}
  • 출력 없음
중급 R

else 문

if(condition) {
  expr1
} else {
  expr2
}
x <- -3
if(x < 0) {
  print("x is a negative number")
} else {
  print("x is either a positive number or zero")
}
"x is a negative number"
중급 R

else 문

if(condition) {
  expr1
} else {
  expr2
}
x <- 5
if(x < 0) {
  print("x is a negative number")
} else {
  print("x is either a positive number or zero")
}
"x is either a positive number or zero"
중급 R

else if 문

if(condition1) {
  expr1
} else if(condition2) {
  expr2
} else {
  expr3
}
중급 R

else if 문

x <- -3
if(x < 0) {
  print("x is a negative number")
} else if(x == 0) {
  print("x is zero")
} else {
  print("x is a positive number")
}
"x is a negative number"
중급 R

else if 문

x <- 0
if(x < 0) {
  print("x is a negative number")
} else if(x == 0) {
  print("x is zero")
} else {
  print("x is a positive number")
}
"x is zero"
중급 R

else if 문

x <- 5
if(x < 0) {
  print("x is a negative number")
} else if(x == 0) {
  print("x is zero")
} else {
  print("x is a positive number")
} 
"x is a positive number"
중급 R

if, elif, else

x <- 6
if(x %% 2 == 0) {
  print("divisible by 2")
} else if(x %% 3 == 0) {
  print("divisible by 3")
} else {
  print("not divisible by 2 nor by 3...")
}
"divisible by 2"
중급 R

연습해 봅시다!

중급 R

Preparing Video For Download...