Chuyển đến nội dung chính
eLearner.app
Mô-đun 2 · Bài học 2 trong tổng số 24/10 trong khóa học~12 min
Bài học theo mô-đun (2/2)

Điều kiện và vòng lặp

To implement complex algorithms and procedures in R, you need to be able to control which instructions to execute and how many times to repeat them.

Conditional Statements

In R, the fundamental conditional structure is based on if and else. The syntax is very similar to languages like JavaScript or C:

Code
score <- 85

if (score >= 60) {
  print("Passed!")
} else {
  print("Failed.")
}

Curly braces {} define the blocks of code to run depending on the outcome of the boolean expression inside parentheses.

The for Loop

The for loop in R is used to iterate over elements in a sequence (such as a vector or list).

Code
# Print numbers from 1 to 3
for (i in 1:3) {
  print(i)
}

In R, the 1:3 syntax is a shorthand way to create the vector c(1, 2, 3).

We can also loop directly over the elements of an existing vector:

Code
fruits <- c("apple", "banana", "cherry")

for (fruit in fruits) {
  print(fruit)
}

The ifelse Function

In R, the ifelse() function is a vectorized version of the if-else statement. It allows you to apply a condition to an entire vector in a single operation, returning a vector of results.

Code
scores <- c(55, 80, 45, 90)
# Se score >= 60 assegna "Pass", altrimenti "Fail"
results <- ifelse(scores >= 60, "Pass", "Fail")
print(results) # "Fail" "Pass" "Fail" "Pass"

The while Loop

The while loop is used to repeat a block of code as long as a specific condition remains true (TRUE). It is essential to ensure that the condition eventually becomes false to prevent infinite loops.

Code
counter <- 1
while (counter <= 3) {
  print(counter)
  counter <- counter + 1
}

Try it yourself

tập thể dục#r.m2.l2.e1
Nỗ lực: 0Đang tải…

Write an if-else conditional statement that checks if the variable score is greater than or equal to 60. If it is, set status to 'Pass', otherwise set it to 'Fail'.

Đang tải trình chỉnh sửa…
Hiển thị gợi ý

Verifica score >= 60 e assegna status <- 'Pass' o status <- 'Fail'

Giải pháp khả dụng sau 3 lần thử

tập thể dục#r.m2.l2.e2
Nỗ lực: 0Đang tải…

Write a for loop that iterates over the sequence 1:5 and prints each value using the print() function.

Đang tải trình chỉnh sửa…
Hiển thị gợi ý

Usa la sintassi: for (i in 1:5) { print(i) }

Giải pháp khả dụng sau 3 lần thử

tập thể dục#r.m2.l2.e3
Nỗ lực: 0Đang tải…

Given the vector v, write a for loop that iterates over each element and prints its double (val * 2) using print().

Đang tải trình chỉnh sửa…
Hiển thị gợi ý

Loop through elements of v: for (val in v) { print(val * 2) }

Giải pháp khả dụng sau 3 lần thử

tập thể dục#r.m2.l2.e4
Nỗ lực: 0Đang tải…

Given the vector scores, use the ifelse() function to check which elements are greater than or equal to 60. Save the result (which will contain 'Pass' or 'Fail' for each element) in the variable results.

Đang tải trình chỉnh sửa…
Hiển thị gợi ý

Use the function: results <- ifelse(scores >= 60, 'Pass', 'Fail')

Giải pháp khả dụng sau 3 lần thử

tập thể dục#r.m2.l2.e5
Nỗ lực: 0Đang tải…

Use a while loop to double the value of x until it exceeds 50. Also increment the counter variable at each iteration.

Đang tải trình chỉnh sửa…
Hiển thị gợi ý

Use a while loop (x <= 50) and update both x and counter inside the curly braces.

Giải pháp khả dụng sau 3 lần thử