menu

Basic R Programs for Beginners

Basic R Programs for Beginners - Important Points


11.

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)
summary(z)

A. A summary of the x and y vectors, including the mean, median, and quartiles of x.

B. A summary of the x and y vectors, including the number of missing values in each column.

C. A summary of the z data frame, including the number of observations, the mean, median, and quartiles of x, and the counts and percentages of each unique value of y.

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 summary function is used to generate a summary of the z data frame, including the number of observations, the mean, median, and quartiles of x, and the counts and percentages of each unique value of y.

12.

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)) + geom_point()

A. A scatterplot of x vs y.

B. A line plot of x vs y.

C. A bar plot of x vs y.

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 ggplot2 library is loaded with the library function. The ggplot function is used to create a plot with z as the data source and aes(x, y) as the mapping of x to the x-axis and y to the y-axis. The geom_point function is used to add points to the plot, resulting in a scatterplot of x vs y.

13.

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. A data frame with two columns: x and y, containing the rows where x is greater than 3.

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

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

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 dplyr library is loaded with the library function. The filter function is used to select rows from the z data frame where x is greater than 3, resulting in a data frame with two columns: x and y, containing the rows where x is greater than 3.

14.

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)
mutate(z, x_squared = x^2)

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

B. A data frame with two columns: x and y, containing the original x and y columns, and no new columns.

C. An error message, because the mutate function can only be used on numeric columns.

D. An error message, because the ^ operator cannot be used on vectors.

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 dplyr library is loaded with the library function. The mutate function is used to add a new column to the z data frame called x_squared, which contains the squares of the x values. The resulting data frame has three columns: x, y, and x_squared.

15.

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. 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.

B. 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.

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

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

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 dplyr library is loaded with the library function. The summarize function is used to generate a summary of the z data frame, including the mean and standard deviation of the x values. The resulting data frame has two columns: mean_x and sd_x.


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