From 1d481d7bca3c03e3f31420a6fbf115bf9667e7cc Mon Sep 17 00:00:00 2001 From: William Okech <38021234+wokech@users.noreply.github.com> Date: Mon, 11 Mar 2024 00:04:28 +0300 Subject: [PATCH] updates by march 10th --- appendix.qmd | 4 ++-- arithmetic_variables.qmd | 48 ++++++++++++++++++++++------------------ data_struct_1.qmd | 44 +++++++++--------------------------- data_struct_2.qmd | 27 +++++++++++++++++----- data_types.qmd | 10 ++++----- download_install.qmd | 2 +- importing.qmd | 18 +++++++++++---- managing.qmd | 16 +++++++------- missing.qmd | 16 +++++++------- operators.qmd | 36 +++++++++++++++--------------- vectors.qmd | 43 ++++++++++++++++++++--------------- 11 files changed, 139 insertions(+), 125 deletions(-) diff --git a/appendix.qmd b/appendix.qmd index 88847de..c5d1b08 100644 --- a/appendix.qmd +++ b/appendix.qmd @@ -26,8 +26,8 @@ To further your R learning journey, I would recommend a review of the following - [R-Ladies Global](https://rladies.org/) - [satRday](https://satrdays.org/) - [rOpenSci](https://ropensci.org/community/) -- **Twitter (now X) – #rstats and #rstudio** -- **Reddit – r/rstats and r/RStudio** +- **Twitter (now X)** – #rstats and #rstudio +- **Reddit** – r/rstats and r/RStudio ## 3. Conferences and Meetups diff --git a/arithmetic_variables.qmd b/arithmetic_variables.qmd index 805b3e5..f904094 100644 --- a/arithmetic_variables.qmd +++ b/arithmetic_variables.qmd @@ -32,9 +32,7 @@ To get started, first, we will open R or RStudio (@fig-arithmetic-1). In R, go to the console, and in RStudio, head to the console pane. Next, type in a basic arithmetic calculation such as `1 + 1` after the angle bracket (`>`) and hit “Enter.” -**NOTE** - -Code Style – have a space on either side of the operator +**NOTE**: According to the Tidyverse Style Guide there should be a space on either side of the operator. Examples @@ -45,11 +43,11 @@ Examples 1 + 1 ``` -The output will be observed next to the square bracket containing the number 1 (\[1\]). The 1 indicates that the indexing begins at position 1. Here, as opposed to Python, which uses zero-based indexing, R uses one-based indexing. +The output will be observed next to the square bracket containing the number 1 (`[1]`). The 1 indicates that the indexing begins at position 1. Here, as opposed to Python, which uses zero-based indexing, R uses one-based indexing. ![A basic arithmetic calculation in the R Console](images/arithmetic/arithmetic_console.png){#fig-arithmetic-1} -Additionally, to include comments into the code block, we use the hash (#) symbol. Anything written after the code block will be commented out and not run. +Additionally, to include comments into the code block, we use the hash (`#`) symbol. Anything written after the code block will be commented out and not run. ```{r} @@ -64,17 +62,17 @@ Various arithmetic operators (listed below) can be used in R/RStudio. | Arithmetic Operator | Description | |:-------------------:|:----------------------------------:| -| \+ | Addition | -| \- | Subtraction | -| \* | Multiplication | -| / | Division | -| \*\* or \^ | Exponentiation | -| %% | Modulus (remainder after division) | -| %/% | Integer division | +| `+` | Addition | +| `-` | Subtraction | +| `*` | Multiplication | +| `/` | Division | +| `**` or `^` | Exponentiation | +| `%%` | Modulus (remainder after division) | +| `%/%` | Integer division | -Examples +Example -Create an R script called `my_arithmetic_1.R` +Create an R script called "my_arithmetic_1.R" Run the following calculations in the script and save the file to the working directory. @@ -128,10 +126,10 @@ Run the following calculations in the script and save the file to the working di ### Variables -Variables are instrumental in programming because they are used as “containers” to store data values. To assign a value to a variable, we can use \<− or =. However, most R users prefer to use \<−. +Variables are instrumental in programming because they are used as "containers" to store data values. To assign a value to a variable, we can use `<−` or `=`. However, most R users prefer to use `<−`. -- Variables store values for later use in your code, improving readability and efficiency. -- Assign values to variables using the \<- operator (e.g., x \<- 5). +- Variables store values for later use in your code. This results in improved readability and efficiency. +- Assign values to variables using the `<-` operator (e.g., `x <- 5`). - Variable names should be descriptive and avoid special characters. #### Variable assignment @@ -165,7 +163,13 @@ variable_4 variable_5 ``` -The output of the variable can then be obtained by: 1. Typing the variable name and then pressing “Enter,” 2. Typing “print” with the variable name in brackets, print(variable), and 3. Typing “View” with the variable name in brackets, View(variable). +The output of the variable can then be obtained by: + +1. Typing the variable name and then pressing “Enter,” + +2. Typing “print” with the variable name in brackets, `print(variable)`, and, + +3. Typing “View” with the variable name in brackets, `View(variable)`. Both `print()` and `View()` are some of the many built-in functions available in R. @@ -181,16 +185,16 @@ View(variable_2) Output of `View()` will be seen in the script pane. -### The `assign()` and `rm()` functions +#### The `assign()` and `rm()` functions -In addition to using the assignment operators (\<- and =), we can use the assign() function to assign a value to a variable. +In addition to using the assignment operators (`<-` and `=`), we can use the `assign()` function to assign a value to a variable. ```{r} assign("variable_6", 555) variable_6 ``` -To remove the assignment of the value to the variable, either delete the variable in the “environment pane” or use the rm() function. +To remove the assignment of the value to the variable, either delete the variable in the “environment pane” or use the `rm()` function. ```{r} variable_7 <- 159 @@ -200,7 +204,7 @@ variable_7 <- 159 rm(variable_7) ``` -After running rm() look at the environment pane to confirm whether variable_7 has been removed. +After running `rm()` look at the environment pane to confirm whether variable_7 has been removed. #### Naming variables diff --git a/data_struct_1.qmd b/data_struct_1.qmd index 1ae6d5e..37d2f0a 100644 --- a/data_struct_1.qmd +++ b/data_struct_1.qmd @@ -130,9 +130,7 @@ length(list_5) Flatten out into a single vector using unlist() ```{r} - unlist(list_5) - ``` ### Dataframes @@ -144,7 +142,7 @@ A data frame is one of the most common data objects used to store tabular data i - Various data can be stored (such as numeric, factor, and character), and, - The individual columns should contain the same number of data items. -**NOTE** Tibbles and their advantages +**NOTE**: Tibbles are the modern versions of dataframes, that have improved printing and subsetting features. The tibble package is a main component of the tidyverse; however, a review of the tidyverse is beyond the scope of this book and will be discussed in subsequent lessons. #### Creation of data frames @@ -158,13 +156,11 @@ age <- c(25, 36, 47) df_1 <- data.frame(level, language, age) ``` -#### Other methods to create dataframes - -stack() / unstack() / cbind() / rbind() - -as.data.frame() +Use `stringsAsFactors = FALSE` to prevent R from converting string columns to factors. -When you use t() on a dataframe, check using class() to ensure that you didn’t convert to a matrix To transpose a dataframe use t() and then as.data.frame() +```{r} +df_2 <- data.frame(level, language, age, stringsAsFactors = FALSE) +``` #### Functions used to manipulate data frames @@ -230,7 +226,7 @@ df_1[3,2] df_1[2, 1:2] ``` -j. Access columns with index / subset +j. Access columns with index ```{r} df_1[, 3] @@ -243,39 +239,21 @@ df_1[, c("language")] k. Access rows with index ```{r} -df_1[2, ] +df_1[2, ] ``` -Other methods for accessing - -df\[c(1:3), \] and df\$col_name and df\[, “\_”\] and df\[ , c(“a”, “b”)\] - -\[ = many elements \[\[ and \$ = single element +**NOTE**: To view the internal structure of various data types described above, the learner can use the `str()` function. -df\[row_indices, column_indices\] - -df\[row_indices, \] - -df\[ , column_indices\] - -which\[ry\$a \> 1\] returns the row indexes of the column “a” that has a value \> 1 +Example ```{r} -ry$a = ry[ , 1] +str(list_3) ``` -attach() - -str() to explain the output - -If you have 4 columns - ```{r} -df[ , c(3,4)] = df[ , -c(1,2)] +str(df_1) ``` -glimpse() / View() / unique() / distinct() complete.cases() - eliminate missing values from a vector, matrix, or data frame - ## Exercises i. Explain the characteristics and use cases of lists in R. diff --git a/data_struct_2.qmd b/data_struct_2.qmd index 1edaeff..aec2bea 100644 --- a/data_struct_2.qmd +++ b/data_struct_2.qmd @@ -54,9 +54,9 @@ A matrix is a rectangular two-dimensional (2D) homogeneous data set containing r a. Creation of matrices -Format: +Using the `matrix()` function -`matrix(range, nrow = _, ncol = _)` +Format: `matrix(range, nrow = _, ncol = _)` ```{r} m1 <- matrix(1:9, nrow = 3, ncol = 3) @@ -67,6 +67,12 @@ m2 m3 ``` +Using `cbind()` and `rbind()` + +```{r} + +``` + b. Obtain the dimensions of the matrices `m1` and `m3` ```{r} @@ -252,12 +258,13 @@ arr_1[, , 1] Factors are used to store integers or strings which are categorical. They categorize data and store the data in different levels. This form of data storage is useful for statistical modelling. Examples include TRUE or FALSE and male or female. Useful for handling qualitative datasets. +**NOTE**: R has a more efficient method for handling categorical variables using the `forcats`package. This will be discussed in a subsequent series focusing on the `tidyverse`. + ```{r} vector <- c("Male", "Female") ``` ```{r} - factor_1 <- factor(vector) factor_1 ``` @@ -273,13 +280,21 @@ factor_2 as.numeric(factor_2) ``` +**NOTE**: To view the internal structure of various data types described above, the learner can use the `str()` function. + +Example + ```{r} -droplevels() +str(m5) ``` -Create example with the following sequence: - as.numeric() - as.character() - factor() +```{r} +str(arr_1) +``` -*NOTE: R has a more efficient method for handling categorical variables using the `forcats`package. This will be discussed in a subsequent series on the `tidyverse`.* +```{r} +str(factor_1) +``` ## Exercises diff --git a/data_types.qmd b/data_types.qmd index eda91ea..65f4e7b 100644 --- a/data_types.qmd +++ b/data_types.qmd @@ -20,9 +20,9 @@ - Learn how to convert between different data types. -- Use functions like class(), typeof(), and str() to inspect types. +- Use functions like `class()`, `typeof()`, and `str()` to inspect types. -- Convert between data types using the as.\* functions. +- Convert between data types using the `as.___()` functions. ## Lesson Content @@ -35,14 +35,14 @@ R and RStudio utilize multiple data types to store different kinds of data. The most common data types in R are listed below. | **Data Type** | **Description** | -|-------------------|-----------------------------------------------------| +|---------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | Numeric | The most common data type. The values can be numbers or decimals (all real numbers). | | Integer | Special case of numeric data without decimals. | | Logical | Boolean data type with only 2 values (`TRUE` or `FALSE`). | | Complex | Specifies imaginary values in R. | | Character | Assigns a character or string to a variable. The character variables are enclosed in single quotes ('character') while the string variables are enclosed in double quotes ("string"). | | Factor | Special type of character variable that represents a categorical such as gender. | -| Raw | Specifies values as raw bytes. It uses built-in functions to convert between raw and character (charToRaw() or rawToChar()). | +| Raw | Specifies values as raw bytes. It uses built-in functions to convert between raw and character (`charToRaw()` or `rawToChar()`). | | Dates | Specifies the date variable. Date stores a date and POSIXct stores a date and time. The output is indicated as the number of days (Date) or number of seconds (POSIXct) since 01/01/1970. | ### Examples of data types in R @@ -263,7 +263,7 @@ Character `>` Numeric `>` Integer `>` Logical - How can you check the data type of a variable in R? -- Use the ```class()```, ```typeof()```, and ```str()``` functions to check the data type of objects. +- Use the `class()`, `typeof()`, and `str()` functions to check the data type of objects. - Describe the process of coercion in R. diff --git a/download_install.qmd b/download_install.qmd index 714c750..1181fd0 100644 --- a/download_install.qmd +++ b/download_install.qmd @@ -68,7 +68,7 @@ Once installed, verify that the RStudio software has a similar graphical user in ### Create an RStudio Cloud Account on Posit website -Posit (formerly RStudio) Cloud lets the user access the RStudio interface from their internet browsers (Figure …). Using this option does not require any installation or specific software configuration to be implemented. Posit Cloud offers a [free plan](https://posit.cloud/plans/free) for casual users (without the need for a paid plan) and there is no need for dedicated hardware. Additionally, Posit provides a comprehensive [guide](https://posit.cloud/learn/guide) for first-time users. +Posit (formerly RStudio) Cloud lets the user access the RStudio interface from their internet browsers (@fig-r-cloud-1). Using this option does not require any installation or specific software configuration to be implemented. Posit Cloud offers a [free plan](https://posit.cloud/plans/free) for casual users (without the need for a paid plan) and there is no need for dedicated hardware. Additionally, Posit provides a comprehensive [guide](https://posit.cloud/learn/guide) for first-time users. #### Step 1 diff --git a/importing.qmd b/importing.qmd index d5ea054..0066673 100644 --- a/importing.qmd +++ b/importing.qmd @@ -36,9 +36,11 @@ A dataset is a collection of data, and R offers the user the opportunity to eith R has several built-in datasets. To access them, we use the `data()` function after loading in the datasets library. -`library(datasets)` +```{r} +library(datasets) -`data()` +data() +``` To find out more about a specific dataset, we use the `?` operator. @@ -54,14 +56,22 @@ Example: #### Loading datasets -`data::dataset` +`datasets::dataset` OR -`data(“dataset”)` +`datasets("dataset")` Example: +```{r} +datasets::airquality +``` + +```{r} +data("airquality") +``` + #### Print datasets `print(dataset)` diff --git a/managing.qmd b/managing.qmd index d1b6a8f..e272d9a 100644 --- a/managing.qmd +++ b/managing.qmd @@ -34,6 +34,10 @@ Good file and data management allows the user to debug code, reduce the number o An R project enables the learner to keep all their work in one folder. In the R project folder, all scripts, data files, figures, tables, history, and output can be stored in sub-folders within the R project. The root folder of the project is also known as the working directory (described in the section below). +To create a new project folder: + +File --\> New Project + ### Scripts To ensure that the learner has a record of all the code that they have written in the console, it is important that the learner also write the code in a script and saves it. Failure to do this will mean that the learner will have to write the code in the console every time they need to execute it. Code written in script files allows one to run it multiple times and edit when necessary. @@ -48,23 +52,19 @@ Ensure that the file names are readable for both the R software and collaborator Examples include: -- "my_model_1.R" +- "my_model_1.R" ### Working Directory The working directory is a file path or location on your computer that R uses for reading and writing files. The function `getwd()` prints the current working directory, and `setwd()` allows the user to set the working directory. This is where R looks for files that you ask it to load, and where it puts any files that you ask it to save. -Relative and absolute paths for loading data - -- Windows = "data\fire.png" - -- Mac/Linux = "data/fire.png" +The paths to files or directories can be defined using relative or absolute paths. The relative path is where the file is found in the location where the user is, and absolute includes the entire path from the root directory. ### Finding help when using packages and functions -- There is a built-in help system that can be accessed via the "Help" menu or one can use the ```?``` symbol before the function, package, or dataset to get a brief description and instructions for use. +- There is a built-in help system that can be accessed via the "Help" menu or one can use the `?` symbol before the function, package, or dataset to get a brief description and instructions for use. -- Online R [documentation](https://cran.r-project.org/manuals.html) is also available to allow the user to learn the basics of R. +- Online R [documentation](https://cran.r-project.org/manuals.html) is also available to allow the user to learn the basics of R. ## Exercises diff --git a/missing.qmd b/missing.qmd index 06e02a4..f8bff55 100644 --- a/missing.qmd +++ b/missing.qmd @@ -26,7 +26,7 @@ - Apply best practices for handling missing data in your R projects. -- Apply functions like is.na() and complete.cases() to identify NA values. +- Apply functions like `is.na()` and `complete.cases()` to identify `NA` values. ## Lesson Content @@ -43,15 +43,15 @@ One of the most common tasks/activities in data analysis is handling missing val Missingness presents itself in a variety of ways in R. The most common way to represent a missing value in R is with the value `NA`. Other reserved values include: -- `NULL` = neither TRUE or FALSE (is.null() identifies a null value and as.null() converts to a null value). +- `NULL` = neither TRUE or FALSE (`is.null()` identifies a null value and `as.null()` converts to a null value). -- `NaN` = impossible values `Inf` = infinite value obtained by dividing by zero (is.infinite() identifies an infinite value and as.infinite() converts to an infinite value). +- `NaN` = impossible values `Inf` = infinite value obtained by dividing by zero (`is.infinite()` identifies an infinite value and `as.infinite()` converts to an infinite value). Examples of the missing values -`NA` = `1/NA` +`NA` = `1/NA` -`NaN` = `0/0` +`NaN` = `0/0` `Inf` = `10/0` @@ -65,15 +65,15 @@ When investigating a dataset, there are a number of functions that can assist wi - Imputation: Replace missing values with estimated values based on statistical methods (can introduce artificial data). -R has two types of missing data, ```NA``` and ```NULL```. +R has two types of missing data, `NA` and `NULL`. Missing values can represent a fixed value like 0 (zero) or a negative number. -Missing can be ```NA``` or ```NaN``` and represented by ```is.na()``` or ```is.nan()``` +Missing can be `NA` or `NaN` and represented by `is.na()` or `is.nan()` ### NA -R uses NA to represent missing data. The NA appears as another element of a vector. To test each element for missingness we use ```is.na()```. Generally, we can use tools such as ```mi```, ```mice```, and ```Amelia``` (which will be discussed later) to deal with missing data. The deletion of this missing data may lead to bias or data loss, so we need to be very careful when handling it. In subsequent blog posts, we will look at the use of imputation to deal with missing data. +R uses NA to represent missing data. The NA appears as another element of a vector. To test each element for missingness we use `is.na()`. Generally, we can use tools such as `mi`, `mice`, and `Amelia` (which will be discussed later) to deal with missing data. The deletion of this missing data may lead to bias or data loss, so we need to be very careful when handling it. In subsequent blog posts, we will look at the use of imputation to deal with missing data. ### NULL diff --git a/operators.qmd b/operators.qmd index f32cab6..3cc61e6 100644 --- a/operators.qmd +++ b/operators.qmd @@ -46,12 +46,12 @@ Relational operators are used to find the relationship between 2 variables and c | Relational Operator | Description | |:-------------------:|:------------------------:| -| \< | Less than | -| \> | Greater than | -| \<= | Less than or equal to | -| \>= | Greater than or equal to | -| == | Equal to | -| != | Not Equal to | +| `<` | Less than | +| `>` | Greater than | +| `<=` | Less than or equal to | +| `>=` | Greater than or equal to | +| `==` | Equal to | +| `!=` | Not Equal to | #### Assign values to variables @@ -102,9 +102,9 @@ Logical operators are used to specify multiple conditions between objects. Logic | Logical Operator | Description | |:----------------:|:------------------------:| -| ! | Logical NOT | -| \| | Element-wise logical OR | -| & | Element-wise logical AND | +| `!` | Logical NOT | +| `|` | Element-wise logical OR | +| `&` | Element-wise logical AND | #### Assign vectors to variables @@ -147,9 +147,9 @@ These are helpful operators for working in that can perform a variety of functio | Miscellaneous Operator | Description | |:----------------------:|:--------------------------------------------------------------:| -| %\*% | Matrix multiplication (to be discussed in subsequent chapters) | -| %in% | Does an element belong to a vector | -| : | Generate a sequence | +| `%*%` | Matrix multiplication (to be discussed in subsequent chapters) | +| `%in%` | Does an element belong to a vector | +| `:` | Generate a sequence | a. Sequence @@ -183,17 +183,17 @@ a %in% b - What operators allow you to subset vectors, lists, and data frames in R? -- Explain how the modulus operator (%/%) differs from the regular division operator (/). +- Explain how the modulus operator (`%/%`) differs from the regular division operator (`/`). -- What are the membership operators %in% and !%in% used for in R? +- What are the membership operators `%in%` and `!%in%` used for in R? -- Assign 10 to x using \<-. Then compare if x == 10. +- Assign 10 to x using `<-`. Then compare if `x == 10`. -- Calculate 3\^4 using arithmetic operators. +- Calculate `3^4` using arithmetic operators. -- Compare 3 \> 5. Is the result TRUE or FALSE? +- Compare `3 > 5`. Is the result TRUE or FALSE? -- When would you use the sequence operator (:) in R? Give an example. +- When would you use the sequence operator (`:`) in R? Give an example. ## Summary diff --git a/vectors.qmd b/vectors.qmd index f471cbf..0a8ef0b 100644 --- a/vectors.qmd +++ b/vectors.qmd @@ -40,7 +40,7 @@ A vector is a collection of elements of the same data type, and they are a basic data structure in R programming. -Vectors cannot be of mixed data type. The most common way to create a vector is with `c()`, where “c” stands for combine. In R, vectors do not have dimensions; therefore, they cannot be defined by columns or rows. Vectors can be divided into atomic vectors and lists (discussed in the "Data Structures" {#sec-data-structure-1} section). The atomic vectors include logical, character, and numeric (integer or double). +Vectors cannot be of mixed data type. The most common way to create a vector is with `c()`, where “c” stands for combine. In R, vectors do not have dimensions; therefore, they cannot be defined by columns or rows. Vectors can be divided into atomic vectors and lists (discussed in the "Data Structures" {@sec-data-structure-1} section). The atomic vectors include logical, character, and numeric (integer or double). Additionally, R is a vectorized language because mathematical operations are applied to each element of the vector without the need to loop through the vector. @@ -66,7 +66,7 @@ c("TRUE", "FALSE", "TRUE") ### Sequence Generation -To generate a vector with a sequence of consecutive numbers, we can use: sequence(), or seq(). +To generate a vector with a sequence of consecutive numbers, we can use: `sequence()`, or `seq()`. Generate a sequence using `:` @@ -99,7 +99,7 @@ c Generate a sequence using `seq()` -The seq() function has four main arguments: seq(from, to, by, length.out), where “from” and “to” are the starting and ending elements of the sequence. Additionally, “by” is the difference between the elements, and “length.out” is the maximum length of the vector. +The `seq()` function has four main arguments: `seq(from, to, by, length.out)`, where "from" and "to" are the starting and ending elements of the sequence. Additionally, "by" is the difference between the elements, and "length.out" is the maximum length of the vector. ```{r} d <- seq(2,20,by=2) @@ -168,8 +168,7 @@ The `runif()` function can be used to generate a specific number of random numbe runif(5, min = 3, max = 100) ``` -**NOTE** -If you run the previous code block multiple times you will get different answers. Use ```set.seed()``` to get a similar sequence every time you run the calculation. +**NOTE** If you run the previous code block multiple times you will get different answers. Use `set.seed()` to get a similar sequence every time you run the calculation. ```{r} set.seed(12345) @@ -421,41 +420,41 @@ IQR() cumsum() -- Range +- Range range() -- Cut +- Cut cut() -- Pretty +- Pretty pretty() - Trigonometric functions -sin() +sin() -cos() +cos() tan() - Logarithmic functions -log() +log() -log2() +log2() log10() -- Miscellaneous functions +- Miscellaneous functions unique() duplicated() -which() +which() is.element() @@ -473,7 +472,7 @@ char_vec_lower <- tolower(char_vec_upper) char_vec_lower ``` -- Create contingency tables +- Create contingency tables Use the table() function to quickly create frequency tables and prop.table() to create a frequency table of proportions. @@ -489,6 +488,8 @@ Create a matrix/dataframe table(vector1, vector2) – vector1 will go in rows an table(cut(vector1), pretty(vector1), vector2) + + 4. Recycling of vectors ```{r} @@ -508,14 +509,16 @@ Examples 1. Name the columns of a vector with names(). -a) Create the vector. + + +a) Create the vector. ```{r} vec_name <- 1:5 vec_name ``` -b) Name the individual elements. +b) Name the individual elements. ```{r} names(vec_name) <- c("a", "c", "e", "g", "i") @@ -557,7 +560,7 @@ vec_index[-2] vec_index[c(2,4)] ``` -d) Modify a vector using indexing +d) Modify a vector using indexing ```{r} vec_index @@ -571,6 +574,10 @@ vec_index[5] <- 1000 vec_index ``` +- Combine vectors by row and column + +cbind() and rbind() + ## Exercises i. Create a numeric vector with values 1 through 5 using c() and extract the middle element from the vector using the described subsetting methods.