位置:首頁 > 大數據教學 > R語言教學 > R語言if語句

R語言if語句

if語句包含一個布爾表達式後跟一個或多個語句。

語法

下麵是在R語言中創建 if語句 的基本語法:

if(boolean_expression) {
   // statement(s) will execute if the boolean expression is true.
}

如果布爾表達式為true,則if語句裡的代碼塊將被執行。如果布爾表達式是false,那麼if語句的第一組碼結束(右大括號後)將被執行。

流程圖

R if statement

示例

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

當上述代碼被編譯和執行時,它產生了以下結果:

[1] "X is an Integer"