반응형

5. If, if else, switch



If 문


x <- 30L
if(is.integer(x)) {
   print("X is an Integer")
}


[1] "X is an Integer"



If else 문


x <- c("what","is","truth")

if("Truth" %in% x) {
   print("Truth is found")
} else {
   print("Truth is not found")
}


[1] "Truth is not found"



Switch 문


switch 문은 일반적인 C, Java등의 프로그래밍 언어와는 조금 다르다.


# by index
switch(1, "one", "two")
## [1] "one"


# by index with complex expressions
switch(2, {"one"}, {"two"})
## [1] "two"


# by index with complex named expression
switch(1, foo={"one"}, bar={"two"})
## [1] "one"


# by name with complex named expression
switch("bar", foo={"one"}, bar={"two"})
## [1] "two"

아래 튜토리얼을 참고한 포스팅입니다. 

https://www.tutorialspoint.com/r/r_operators.htm 

반응형

'Tools > R' 카테고리의 다른 글

R - (7) 함수  (0) 2017.02.19
R - (6) 반복문  (0) 2017.02.17
R - (4) 연산자  (0) 2017.02.15
R - (3) 변수  (0) 2017.02.15
R - (2) R의 데이터 타입 (자료구조)  (0) 2017.02.13
반응형