Q: How do you calculate the mean of a dataset in R?
Answer: To calculate the mean (average) value of a dataset in R, use the mean() function.
This function sums all the values in the dataset and divides by the number of values.
mean(data)
Example:
data <- c(1, 2, 3, 4, 5)
mean(data)
# Output: 3
Q: What function calculates the median of a dataset?
Answer : To find the median (the middle value) of a dataset, use the median() function.
The function sorts the data and returns the middle value, or the average of the two middle
values if the data length is even.
median(data)
Example:
data <- c(1, 2, 3, 4, 5)
median(data)
# Output: 3
data <- c(1, 2, 3, 4, 5, 6)
median(data)
# Output: 3.5
Q: How can you find the variance of a dataset?
Answer: To measure the spread of data points around the mean, use the var() function.
Variance is calculated as the average of the squared differences from the mean.
var(data)
Example:
data <- c(1, 2, 3, 4, 5)
var(data) # Output: 2.5
Q: Which function computes the standard deviation in R?
Answer: The sd() function calculates the standard deviation, which is the square root of
the variance. It measures the amount of variation or dispersion in a dataset.
sd(data)
Example:
data <- c(1, 2, 3, 4, 5)
sd(data)
# Output: 1.581139
Q: How do you determine the range of a dataset?
Answer: To get the minimum and maximum values of a dataset, use the range() function.
The range is the difference between the maximum and minimum values.
range(data)
Example:
data <- c(1, 2, 3, 4, 5)
range(data)
# Output: 1 5
Q: What is the function to calculate the interquartile range (IQR) in R?
Answer: The IQR() function measures the statistical dispersion, which is the difference
between the 75th (Q3) and 25th (Q1) percentiles. It is useful for understanding the spread
of the middle 50% of the data.
IQR(data)
Example:
data <- c(1, 2, 3, 4, 5)
IQR(data)
# Output: 2
Q: How can you create a histogram in R?
Answer: Use the hist() function to create a histogram, which shows the frequency
distribution of a dataset. Customize the title and axis labels with the main, xlab, and ylab
parameters.
hist(data, main="Histogram", xlab="Data", ylab="Frequency")
Example:
data <- rnorm(100)
hist(data, main="Histogram of Random Data", xlab="Value", ylab="Frequency")
Q: What function is used to generate a boxplot in R?
Answer: The boxplot() function creates a boxplot, displaying the distribution of data
based on a five-number summary: minimum, first quartile (Q1), median, third quartile
(Q3), and maximum.
boxplot(data, main="Boxplot", ylab="Data")
Example:
data <- rnorm(100)
boxplot(data, main="Boxplot of Random Data", ylab="Value")
Q: How do you create a scatter plot in R?
Answer: Use the plot() function to create a scatter plot, which shows the relationship
between two quantitative variables. Customize the plot with titles and axis labels.
plot(x, y, main="Scatter Plot", xlab="X-Axis", ylab="Y-Axis")
Example:
x <- rnorm(100)
y <- rnorm(100)
plot(x, y, main="Scatter Plot of Random Data", xlab="X Values", ylab="Y Values")
Q: How do you create faceted plots using ggplot2?
Answer: Use the facet_wrap() function in ggplot2 to split data into subsets and display
them in separate plots within the same graph. This is useful for comparing different
groups.
ggplot(data, aes(x, y)) +
geom_point() +
facet_wrap(~ factor)
Example:
library(ggplot2)
data <- data.frame(x = rnorm(100), y = rnorm(100), group = rep(1:2, each=50))
ggplot(data, aes(x, y)) +
geom_point() +
facet_wrap(~ group)
Q: Which function can you use to apply a minimal theme in ggplot2?
Answer: Use the theme_minimal() function to apply a clean, minimalistic theme to the
plot, removing unnecessary background elements.
ggplot(data, aes(x, y)) +
geom_point() +
theme_minimal()
Example:
ggplot(data, aes(x, y)) +
geom_point() +
theme_minimal()
Q: How do you add annotations to a ggplot2 plot?
Answer: Use the annotate() function to add text or labels to a plot. Specify the type of
annotation (e.g., "text"), the x and y coordinates, and the label.
ggplot(data, aes(x, y)) +
geom_point() +
annotate("text", x = x_pos, y = y_pos, label = "Label")
Example:
ggplot(data, aes(x, y)) +
geom_point() +
annotate("text", x = 0, y = 0, label = "Center Point")
Q: What is the method for applying custom color scales in ggplot2?
Answer: Use the scale_color_manual() function to change the color scheme of your plot
for better visualization. Specify the colors using the values parameter.
ggplot(data, aes(x, y, color = factor)) +
geom_point() +
scale_color_manual(values = c("red", "blue", "green"))
Example:
data <- data.frame(x = rnorm(100), y = rnorm(100), group = rep(1:3, length.out = 100))
ggplot(data, aes(x, y, color = as.factor(group))) +
geom_point() +
scale_color_manual(values = c("red", "blue", "green"))
Q: How can you save a ggplot2 plot to a file?
Answer: Use the ggsave() function to export the plot to various file formats, such as
PNG, PDF, or JPEG. Specify the filename and the plot object if needed.
ggsave("plot.png", plot = last_plot(), device = "png")
Example:
p <- ggplot(data, aes(x, y)) + geom_point()
ggsave("my_plot.png", plot = p, device = "png")