menu

Basic R Programs for Beginners

Basic R Programs for Beginners - Important Points


6.

What is the output of the following R code snippet?

x <- c(1, 2, 3, 4, 5)
y <- c(2, 3, 4)
z <- x[!x %in% y]
print(z)

A. 1 2 3

B. 1 5

C. 2 3 4

D. 4 5

Discuss Work Space

Answer: option b

Explanation:

The code defines two vectors x and y, then uses the %in% operator to check which elements of x are also present in y, negates the result using the ! operator, and selects the elements of x that are not in y using square brackets. The resulting vector is [1 5].

7.

What is the output of the following R code snippet?

x <- 1
while (x < 5) {
print(x)
x <- x + 1
}

A. 1 2 3 4

B. 2 3 4 5

C. 1 2 3 4 5

D. Error

Discuss Work Space

Answer: option a

Explanation:

The code uses a while loop to repeatedly print the value of x as long as x is less than 5, and increments x by 1 in each iteration. The loop executes 4 times and prints the values 1, 2, 3, 4.

8.

What is the output of the following R code snippet?

x <- 1
for (i in 1:5) {
x <- x * i
}
print(x)

A. 5

B. 24

C. 120

D. 720

Discuss Work Space

Answer: option c

Explanation:

The code uses a for loop to multiply x by the integers from 1 to 5 and store the result back in x. After the loop completes, x contains the product of the integers from 1 to 5, which is 120.

9.

What is the output of the following R code snippet?

x <- c(1, 2, 3, 4, 5)
y <- c("one", "two", "three", "four", "five")
z <- data.frame(x, y)
subset(z, x %% 2 == 0)

A. A data frame with two columns: x and y, with the rows where x is even.

B. A data frame with two columns: x and y, with the rows where y is even.

C. A data frame with two columns: x and y, with the rows where both x and y are even.

D. Error

Discuss Work Space

Answer: option a

Explanation:

The code defines two vectors x and y, then combines them into a data frame using the data.frame function. The subset function is used to select only the rows of the data frame where x is even, resulting in a data frame with two columns: x and y, containing the rows where x is even.

10.

What is the output of the following R code snippet?

x <- c(1, 2, 3, 4, 5)
y <- c("one", "two", "three", "four", "five")
z <- data.frame(x, y)
z$y <- as.factor(z$y)
levels(z$y)

A. 1 "2" "3" "4" "5"

B. one "two" "three" "four" "five"

C. five "four" "one" "three" "two"

D. Error

Discuss Work Space

Answer: option c

Explanation:

The code defines two vectors x and y, then combines them into a data frame using the data.frame function. The as.factor function is used to convert the y column to a factor, which creates levels for each unique value of y. The levels function is used to extract the levels of the y factor, which are the original character values "five" "four" "one" "three" "two".


Subscribe for Latest Career Trends
Subscribe Now
Use AI and ChatGPT for Career Guidance

Unlock Your Future

Join Now
Worried for Placements in 2024?

Join FAST TRACK Course

Join Now
Supercharge Your SUCCESS

Join All in One Placement Mock Tests-2024

Join Now