Читать книгу The Big R-Book - Philippe J. S. De Brouwer - Страница 130

Function use for if()

Оглавление

if (logical statement) { executed if logical statement is true } else { executed if the logical statement if false }

Note that the else-statement is optional.

This basic construct can also be enriched with else if statements. For example, we draw a random number from the normal distribution and check if it is bigger than zero.

set.seed(1890) x <- rnorm(1) if (x < 0) { print(‘x is negative’) } else if (x > 0) { print(‘x is positive’) } else { print(‘x is zero’) } ## [1] “x is positive”

The Big R-Book

Подняться наверх