Week 11 Exercises: R Basics

Authors
Affiliation

Menuka Bhandari

Jelmer Poelstra

Published

November 3, 2025



1 Setting up

2 Part A: Create variables and perfrom arithmetic operations

  1. Assign the value 5 to the variable week11, and 10 to the variable week12. Print these variables to the console.

  2. Add the values of week11 and week12 and assign the result to to a new variable total_weeks. Similarly, multiply the values of week11 and week12 and assign the result to a new variable multiplied_weeks. Print these variables to the console.

3 Part B: Create vectors and name its elements

  1. Create a vector v1 that contains the numbers 1 through 5. Then, extract the third element of v1, printing it to the console. Likewise, create another variable v2 with numbers 6 to 10. Check what data types v1 and v2 contain.

  2. Change the name of the third element of v1 to “Midweek” and print v1. Check the data type of v1 after changing third element to Midweek. Try to change the v1 to numeric and see what happens.

  3. Create a vector v3 that combines v1 and v2. Extract the third, fourth and fifth element of v3 and assign it to v4. Print v4 to the console.

4 Part C: Install and load new packages in R

  1. Install the package palmerpenguins and load the package in R. We will use this package in week13 for data visualization.

5 Part D: Working with matrix and dataframes

  1. Look for help for matrix function and create a matrix with numbers 1 to 10 with 2 rows and 5 columns and assign it to a new object called my_matrix.

  2. Last week we worked with the iris data frame, which is one of the data sets that comes with R. For this exercise, we will use the mtcars data frame, which similarly comes with R. Start by storing mtcars in a new object called my_cars, then view the first 6 rows of my_cars.

  3. Check the structure of the my_cars data frame. Structure of data shows the datatypes of different variables in the data frame. Compute the mean of the mpg variable of my_cars data frame.

  4. Show the first 6 rows of the my_cars data frame. Select columns mpg, cyl, and hp from the my_cars data frame and display the first 6 rows.

    Remember to load the tidyverse package before using the select() function.

  5. Write a single “pipeline” that manipulates my_cars by:

    • Selecting columns mpg (miles per gallon) and cyl (number of cylinders)
    • Only keeping rows with mpg greater than 10 and cyl equal to 6.
    • Renaming the cyl column to cylinders
    • Assigning the resulting data frame to an object called my_cars_filtered
  6. Save the new_data frame as a CSV file called my_cars_filtered.csv in your working directory. In the RStudio “Files” pane, check that the file is there and click on it to view it in the editor pane.

6 Solutions

6.1 Part A: Create variables and perform arithmetic operations

1. Assign values to variables
week11 <- 5
week11
[1] 5
week12 <- 10
week12
[1] 10
2. Arithmetic operations
total_weeks <- week11 + week12
total_weeks
[1] 15
multiplied_weeks <- week11 * week12
multiplied_weeks
[1] 50

6.2 Part B: Create and name a vector

3. Create vectors and extract elements
v1 <- 1:5
v1[3]
[1] 3
v2 <- 6:10

str(v1)
 int [1:5] 1 2 3 4 5
str(v2)
 int [1:5] 6 7 8 9 10
4. Change vectors

After replacing a numeric element with a string, the entire vector becomes a character vector And when you try to convert it back to numeric, R will return NA for the non-numeric elements:

v1[3] <- "Midweek"
v1
[1] "1"       "2"       "Midweek" "4"       "5"      
str(v1)
 chr [1:5] "1" "2" "Midweek" "4" "5"
as.numeric(v1)
Warning: NAs introduced by coercion
[1]  1  2 NA  4  5
5. Combine vectors
v3 <- c(v1, v2)
v4 <- v3[3:5]
v4
[1] "Midweek" "4"       "5"      

6.3 Part C: Install and load new packages in R

6. Install and load a package
install.packages("palmerpenguins")
library(palmerpenguins)

6.4 Part D: Working with matrix and dataframes

7. Create a matrix
?matrix()
my_matrix <- matrix(1:10, nrow=2, ncol = 5)
my_matrix
8. Create object
my_cars <- mtcars

head(my_cars)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
9. Structure of data frame
str(my_cars)
'data.frame':   32 obs. of  11 variables:
 $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
 $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
 $ disp: num  160 160 108 258 360 ...
 $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
 $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
 $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
 $ qsec: num  16.5 17 18.6 19.4 17 ...
 $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
 $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
 $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
 $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
mean(my_cars$mpg)
[1] 20.09062
10. Data manipulation
head(my_cars)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.2
✔ ggplot2   3.5.2     ✔ tibble    3.3.0
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.1.0     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
my_cars |>
  select(mpg, cyl, hp) |> 
  head()
                   mpg cyl  hp
Mazda RX4         21.0   6 110
Mazda RX4 Wag     21.0   6 110
Datsun 710        22.8   4  93
Hornet 4 Drive    21.4   6 110
Hornet Sportabout 18.7   8 175
Valiant           18.1   6 105
11. Data manipulation and assign data to new object
my_cars_filtered.csv <- my_cars |> 
  select(mpg, cyl) |>
  filter(mpg > 10, cyl == 6 ) |>
  rename(cylinders = cyl)

my_cars_filtered.csv
                mpg cylinders
Mazda RX4      21.0         6
Mazda RX4 Wag  21.0         6
Hornet 4 Drive 21.4         6
Valiant        18.1         6
Merc 280       19.2         6
Merc 280C      17.8         6
Ferrari Dino   19.7         6
13. Save data in your working directory
write_csv(my_cars_filtered.csv, "my_cars_filtered.csv")
Back to top