Examveda

How do you create a vector containing the first 5 positive even numbers in R using a loop?

A. even_numbers <- numeric(5); for (i in 1:5) { even_numbers[i] <- 2 * i }

B. even_numbers <- for (i in 1:5) { 2 * i }

C. even_numbers <- seq(2, 10, by = 2)

D. even_numbers <- c(2, 4, 6, 8, 10)

Answer: Option A


Join The Discussion

Related Questions on Control Structures in R Programming

In R, what is the purpose of the if-else statement?

A. To execute a block of code if a condition is true, otherwise execute another block of code

B. To create a loop that iterates a specified number of times

C. To check multiple conditions sequentially

D. To execute a block of code repeatedly until a condition is met