R Loops & Conditionals: Complete Guide
In R language, you can use if statements for conditional evaluation, and control loops with for loops, while loops, repeat loops, and similar statements.
- Make conditional statements using if statements.
x <- 10
if (x > 5) {
print("x大于5")
} else {
print("x小于等于5")
}
- Control the loop using a for loop.
for (i in 1:5) {
print(i)
}
- Control the loop using a while loop.
i <- 1
while (i <= 5) {
print(i)
i <- i + 1
}
- Use the repeat loop for loop control.
i <- 1
repeat {
print(i)
i <- i + 1
if (i > 5) {
break
}
}