Assignment 3: R Basics & GitHub Copilot

Author

Matthew DeHaven

Published

February 9, 2026

1 Accept Assignment on Github Classroom

  1. Accept Assignment 3 on Github Classroom

  2. Clone this assignment to your computer.

2 Subsetting data.frames

Open up the file “r-basics-dataframes.r” and you should see the following data.frame:

df <- data.frame(
  x = c(1, 5, 10, 20, 3, 4, 8, 99),
  y = c(2, 3, 8, 10, 23, 89, 200, 72),
  group = c("C", "C", "B", "C", "B", "B", "A", "A")
)

Subset this data.frame using the following methods (add your code to the “r-basics-dataframes.r” file):

  • Subset the 4th row, 2nd column element using numerical indexes
  • Subset the 4th to 7th row, 2nd column elements by passing a vector of numerical indexes

We can also subset a data.frame by using a vector of booleans (TRUE/FALSE values) that are the same length as the number of rows (or columns if subsetting columns).

  • Write a logical test for if the “group” column equals ‘B’
  • Use that expression to subset the data.frame on rows, and the 2nd column
  • Take the average of all of the “y” values for which the “group” equals ‘C’

3 Factors

Factors are another data structure that we mentioned but did not cover in class.

Use the following code to create a factor using the values from our data.frame.

my_factor <- factor(df$group)
  • Print the factor. What new information do you see?

  • Call str() on my_factor. What do you notice about the stored values?

Factors supply ordering to string variables. This is very useful for data such as the responses to a survey: i.e. “Very Good”, “Good”, “Neutral”, “Bad”, “Very Bad”.

  • Check the documentation for factor. What are the other arguments that can be prodvided?

  • Use the levels argument to pass a new set of levels, including a level “D” which is not in the data.

my_factor2 <- 

Print the factor, note the additional level.

  • Use the levels argument to pass a new set of levels, with only “A” and “B”.
my_factor3 <-

Print the factor. What happened to the “C” observations?

Sometimes you want to pass an explicit ordering of levels to construct a factor.

  • Using the documentation, create a factor with levels ordered as B < C < A.
my_factor4 <- 
  • Find the function for sorting vectors, and use that one each of your factors (my_factorX) and on the vector df$group.

4 Loops

Open up the script “r-basics-loops.r”.

A Bad While Loop

  • Go ahead and run the first while(){} loop. Yes, it will go on forever, printing out a “.” every second.

  • Find the button to kill this R terminal in VS Code. 1

1 It looks like a trashcan.

Sometimes you will have run away processes like this where they were coded poorly. Or you realize your code will finish in 2 weeks and you should probably find a more efficient method.

  • Change the “TRUE” value in the while(){} loop to FALSE so it won’t run.

Writing a For Loop

The next exercise is going to ask you to write your own for(){} loop and if(){} statements.

I have written the following code that randomly draws 10000 times from the Normal distribution and stores the result in variable x.

set.seed(42)

x <- rnorm(10000)
groups <- rep(NA, 10000)

The set.seed() function makes random draws reproducible so we will all draw the same random 10000 numbers.

  • Write a for loop that traverses each element in x and assign a group to same the index in the vector groups based off of the following rules:
    • Assign group ‘A’ if the value is in \((-\infty, -2)\)
    • Assign group ‘B’ if the value is in \([-2,-1)\)
    • Assign group ‘C’ if the value is in \([-1,0)\)
    • Assign group ‘D’ if the value is in \([0, 2)\)
    • Assign group ‘E’ otherwise

There are much better ways to code this type of task, but we are practicing for loops and if statements.

Tip

While it is simplest to iterate over the values in x, it is often much more convenient to loop over the indexes.

  • Run table(groups), you should get the following result:
table(groups)
groups
   A    B    C    D    E 
 236 1412 3385 4742  225 

5 GitHub Copilot

5.1 Setup GitHub Copilot

Instructions for setting up GitHub Copilot

You will need:

6 Regex

  • Open up the file “r-regex.r”.

  • Create a regex pattern to extract the dates from the text

7 Submitting Your Assignment

  • commit and push all of your final changes!