menu

Basic R Programs for Beginners


1.

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)
library(ggplot2)
ggplot(z, aes(x = y, y = x)) + geom_bar(stat = "identity")

A bar chart with bars at the y-axis labels "one", "two", "three", "four", and "five", with heights corresponding to the x values in the z data frame.

A bar chart with bars at the x-axis labels "one", "two", "three", "four", and "five", with heights corresponding to the y values in the z data frame.

An error message, because ggplot2 cannot be used with non-numeric data.

An error message, because the geom_bar function cannot be used with non-numeric data.


2.

What is the output of the following R code snippet?

x <- c(1, 2, 3, 4, 5)
y <- sum(x)
print(y)

1

5

15

Error


3.

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)
library(ggplot2)
ggplot(z, aes(x = y, y = x)) + geom_boxplot()

A boxplot with boxes at the y-axis labels "one", "two", "three", "four", and "five", with widths corresponding to the x values in the z data frame.

A boxplot with boxes at the x-axis labels "one", "two", "three", "four", and "five", with heights corresponding to the y values in the z data frame.

An error message, because ggplot2 cannot be used with non-numeric data.

An error message, because the geom_boxplot function cannot be used with non-numeric data.


4.

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)
library(dplyr)
filter(z, x > 3)

A data frame with two columns: x and y, containing the rows where x is greater than 3.

A data frame with two columns: x and y, containing the rows where y is greater than 3.

A data frame with two columns: x and y, containing the rows where x and y are greater than 3.

Error


5.

What is the output of the following R code snippet?

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

1 2 3 4

2 3 4 5

1 2 3 4 5

Error


6.

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)

1 "2" "3" "4" "5"

one "two" "three" "four" "five"

five "four" "one" "three" "two"

Error


7.

What is the output of the following R code snippet?

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

5

24

120

720


8.

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)
library(dplyr)
summarize(z, mean_x = mean(x), sd_x = sd(x))

A data frame with two columns: mean_x and sd_x, containing the mean and standard deviation of the x values in the z data frame.

A data frame with three columns: x, y, and mean_x, containing the original x and y columns, and a new column mean_x with the mean of the x values.

An error message, because the summarize function can only be used on grouped data.

An error message, because the mean and sd functions cannot be used on character columns.


9.

What is the output of the following R code snippet?

x <- "Hello"
y <- "World"
z <- paste(x, y)
print(z)

HelloWorld

Hello World

Error

NA


10.

What is the output of the following R code snippet?

x <- 1:10
y <- x[x %% 2 == 0]
print(y)

1 3 5 7 9

2 4 6 8 10

1 2 3 4 5

2 3 5 7 11