week11 <- 5
week11[1] 5
week12 <- 10
week12[1] 10
November 3, 2025
At https://ondemand.osc.edu, start a new RStudio session (Starting an RStudio Server session at OSC)
Switch to your week11 RStudio Project (Working directory and RStudio Projects)
Open a new R script in RStudio: File -> New File -> R Script and save the R script as w11_exercises.R (inside week11). You will write your answers to the exercises below in this R script.
Assign the value 5 to the variable week11, and 10 to the variable week12. Print these variables to the console.
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.
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.
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.
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.
palmerpenguins and load the package in R. We will use this package in week13 for data visualization.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.
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.
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.
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.
Write a single “pipeline” that manipulates my_cars by:
mpg (miles per gallon) and cyl (number of cylinders)mpg greater than 10 and cyl equal to 6.cyl column to cylindersmy_cars_filteredSave 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.
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:
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
'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 ...
[1] 20.09062
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
── 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
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