ruk·si

📊 R
Flow Control

Updated at 2017-10-13 17:52

If-statement.

x <- 5
if (x > 0) {
  print("x is positive")
} else if (x < 0) {
  print("x is negative")
} else {
  print("x is zero")
}
# [1] "x is positive"

For-loop.

for (i in 1:3) {
  print(i)
}
# [1] 1
# [1] 2
# [1] 3

While-loop.

x <- 3
while (x > 0) {
  print(x)
  x <- x - 1
}
# [1] 3
# [1] 2
# [1] 1

Errors are raised with stop.

stop("File is in wrong format.")
# Error: File is in wrong format.
# Execution halted

Sources

  • Google Developers R Programming Videos
  • Try R