Tutorial 01: Introduction to R
Q1 — Make the sum equal 10
Replace _ with a number so the expression evaluates to 10.
Replace the two _ with 2 numbers that add up to 7.
1 + 2 + 3 + 4
1 + 2 + 3 + 4Q2 — Create a vector
Make a vector called my_vec that contains the numbers 5, 10, 15, 20.
Use the c() function to combine numbers: c(1, 2, 3)
my_vec <- c(5, 10, 15, 20)
my_vec <- c(5, 10, 15, 20)Q3 — Find the average
Compute the mean of the vector c(2, 4, 6, 8, 10).
Your answer should be stored in a variable called avg_val.
Use the mean() function: mean(c(…))
avg_val <- mean(c(2, 4, 6, 8, 10))
avg_val <- mean(c(2, 4, 6, 8, 10))Q4 — Draw a histogram from ToothGrowth (type the full call)
Use exactly one of these numeric columns and nothing else: len or dose
Find a preview of the ToothGrowth dataset here:
Use the hist command.
hist(ToothGrowth$len)
hist(ToothGrowth$len)Q5 — Draw a boxplot from ToothGrowth (type the full call)
Use exactly one of these numeric columns and nothing else: len or dose
Find a preview of the ToothGrowth dataset here:
Use the boxplot command.
boxplot(ToothGrowth$len)
boxplot(ToothGrowth$len)Q6 — Add a color to the histogram
Re-draw a histogram of ToothGrowth$len and set any bar color using the col= argument.
add col as an additional argument here and give it a color of your choice.
hist(ToothGrowth$len, col = "steelblue")
hist(ToothGrowth$len, col = "steelblue")Q7 — Boxplot (make it horizontal)
Draw a boxplot of ToothGrowth$dose and make it horizontal using horizontal = TRUE as an argument.
add horizontal as an argument just like col from the previous question
boxplot(ToothGrowth$dose, horizontal = TRUE)
boxplot(ToothGrowth$dose, horizontal = TRUE)Q8 — Load a CSV as a DataFrame
Load iris.csv into a dataframe named df.
Use read.csv here.
df <- read.csv("iris.csv")
df <- read.csv("iris.csv")Q9 — Count rows in iris.csv
Count the number of rows in iris.csv and store them in the variable rows
Use nrow.
df <- read.csv("iris.csv")
rows <- nrow(df)
df <- read.csv("iris.csv")
rows <- nrow(df)Q10 — Show column names
Print the column names from iris.csv
Use names.
df <- read.csv("iris.csv")
cols <- names(df)
cols
df <- read.csv("iris.csv")
cols <- names(df)
colsQ11 — Print some of the data
Print the first 5 rows from iris.csv.
Use head.
df <- read.csv("iris.csv")
head(df)
df <- read.csv("iris.csv")
head(df)Q12 — Make a histogram from iris.csv
Make a histogram of the column Sepal.Length from iris.csv.
Use hist (see Q5).
df <- read.csv("iris.csv")
hist(df$Sepal.Length, col = "lightgray", main = "Sepal.Length", xlab = "cm")
df <- read.csv("iris.csv")
hist(df$Sepal.Length, col = "lightgray", main = "Sepal.Length", xlab = "cm") Q13 — Write a function using division
Define a function called divide_nums that takes two arguments (a and b) and returns the results of:
a / bb / a
Both results should be stored in separate variables before being returned.
Remember: in R, division by zero (1/0) will return Inf rather than an error.
In R, lists are written with list(a, b, c). Each element can be anything: a number, a string, or even another list.
Start with: res1 <- a / b res2 <- b / a and then use the list command.
divide_nums <- function(a, b) {
res1 <- a / b
res2 <- b / a
list(res1, res2)
}
divide_nums <- function(a, b) {
res1 <- a / b
res2 <- b / a
list(res1, res2)
}