BGEN516 - Intro to R Lab

Author
Affiliation

University of Montana

Published

September 15, 2025

Lab Overview

In this lab, I’ll create three vectors for different data types: numeric, character, and logical. I’ll then evaluate the vectors for data type and missing values before moving on add new values to each vector.

Create Vectors

In this section of my lab, I’ve created a set of related vectors describing members of my household.

Note that I’ve told R that it shouldn’t display my R code in this section when it renders the Quarto document by specifying FALSE for the include argument in each code chunk.

Although the code is not displayed in the rendered document, R still evaluates (runs) the code which allows me to refer to it in the remainder of this document.

Specifically, I’ve told R to display the output of my code in the table below via the use of inline R code.

Vector Description Data Type Values
names given name of household member character tyler, olivia, josie
height_m height of household member in meters double 1.905, 1.6256, 0.2286
is_cat household member is of the species Felis catus logical FALSE, FALSE, TRUE

Check Vectors

In the table above, I displayed the data type for each vector. But I’ll do it again in this section within a code chunk.

What type of data to my vectors contain?

Starting with names, then height_m, and finally, is_cat.

[1] "character"
[1] "double"
[1] "logical"

Do my vectors contain any missing values?

Again, starting with names, then height_m, and finally, is_cat.

[1] FALSE FALSE FALSE
[1] FALSE FALSE FALSE
[1] FALSE FALSE FALSE

Modify Vectors

Now I’m going to add new values to each vector. Check out my code comments to see why.

See table below for additions made to vectors. Again I’ve used inline R code to insert data/results in the formatted text of my document.

Vector New Values
names theo, …
height_m 0.254, NA
is_cat TRUE, NA

Subset Vectors

Finally, I’ll extract some values from my vectors. Let’s say I’m only interested in the cat data in my vectors. So I’ll only keep that data.

Note that, within this section, my R code is displayed. By default, the include and echo options for R code chunks are set to TRUE which means both R code and results will be displayed in my rendered document.

It also means that I don’t have to tell R to display them (i.e., I don’t need to add include=TRUE or echo=TRUE to my code chunks).

Indices and conditions

First, I’m going to use indices and conditional subsetting to create new vector objects.

# subset three values from vectors
cat_names <- names[3:5]
cat_height_m <- height_m[height_m < 1.0 | is.na(height_m)]
cat_check <- is_cat[3:5]

Let’s check out the new vectors.

cat_names
[1] "josie" "theo"  "..."  
cat_height_m
[1] 0.2286 0.2540     NA
cat_check
[1] TRUE TRUE   NA

Rounding

Second, I’m going to keep a single value from my numeric vector. Specifically, I want to check the height of the smallest member of my household, Josie.

Note that I can do this with either my original vector height_m or my new vector cat_height because they both contain Josie’s data.

I’ll use the position of her name to select her height. I can do this because I know the order of the data is consistent across my vectors (i.e., her data is at the same position in both the names vector and the height_m vector). But it’s not really a robust approach.

Because she is the smallest member of my household, I know that her height will be the smallest value in the numeric vector. So an alternative and more robust approach is to extract the smallest value from the numeric vector. I’ll need to make sure to remove NAs from the evaluation, too.

I’m going to store her height as an object and then apply the round function to the object.

# using original vector and its values
josie_height_1 <- height_m[names == "josie"]
round(josie_height_1, digits = 2)
[1] 0.23
# using new vectors and smallest value 
josie_height_2 <- min(cat_height_m, na.rm=T)
round(josie_height_2, digits = 2)
[1] 0.23

That wraps up this lab!