Lab 3: Generating Sequences of Numbers

BI 255 · Bethune-Cookman University · Fall 2026

Author

Dr. Rosie Stanbrook-Buyer

Published

August 24, 2026


Sequences and the NBA

An 82-game NBA season generates an enormous amount of data — game numbers, jersey numbers, player ages, season years. In R, you don’t have to type every number by hand. R gives you powerful tools to generate sequences automatically.

Today we’ll learn three ways to create sequences: the : operator, seq(), and rep().

NoteLearning Objectives

By the end of this lab you will be able to:

  1. Use the : operator to create simple integer sequences
  2. Use seq() to create flexible sequences with custom spacing
  3. Use rep() to repeat values
  4. Combine sequences with c()
  5. Apply simple operations across an entire sequence at once

Part 1: The : Operator — The Quick Sequence

The colon : is the fastest way to make a sequence of whole numbers.

# NBA games are numbered 1 through 82 in a regular season
game_numbers <- 1:82
game_numbers
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
[51] 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
[76] 76 77 78 79 80 81 82
# How many games are there?
length(game_numbers)
[1] 82
# You can also go backwards
countdown <- 10:1
countdown
 [1] 10  9  8  7  6  5  4  3  2  1
# Sequences don't have to start at 1
jersey_numbers <- 23:32   # Numbers around LeBron James' famous #23
jersey_numbers
 [1] 23 24 25 26 27 28 29 30 31 32
TipWhen to Use :

Use : when you need consecutive whole numbers (1, 2, 3, …).

It’s great for: - Game numbers in a season - Row numbers in a dataset - Counting iterations


Part 2: seq() — The Flexible Sequence

When you need more control — different step sizes, specific endpoints, or a set number of values — use seq().

seq() with a step size (by =)

# Every 5th jersey number from 0 to 99 
every_fifth <- seq(from = 0, to = 99, by = 5)
every_fifth
 [1]  0  5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95
# Quarter times in a basketball game (12-minute quarters)
# Generate the minute marks at the end of each quarter
quarter_ends <- seq(from = 12, to = 48, by = 12)
quarter_ends
[1] 12 24 36 48

seq() with a fixed number of values (length.out =)

# Suppose we want exactly 10 evenly spaced points
# between a player's rookie salary ($1M) and max salary ($50M)
salary_steps <- seq(from = 1000000, to = 50000000, length.out = 10)
salary_steps
 [1]  1000000  6444444 11888889 17333333 22777778 28222222 33666667 39111111
 [9] 44555556 50000000
# Round to nearest dollar
round(salary_steps, 0)
 [1]  1000000  6444444 11888889 17333333 22777778 28222222 33666667 39111111
 [9] 44555556 50000000
Noteseq() Arguments Summary
Argument Meaning Example
from Starting value from = 0
to Ending value to = 100
by Step between values by = 5
length.out Total number of values length.out = 10

You use either by or length.out — not both.

# NBA three-point line is 23.75 feet from the basket
# How many attempts from 15 to 30 feet in 0.5-foot increments?
distances <- seq(15, 30, by = 0.5)
distances
 [1] 15.0 15.5 16.0 16.5 17.0 17.5 18.0 18.5 19.0 19.5 20.0 20.5 21.0 21.5 22.0
[16] 22.5 23.0 23.5 24.0 24.5 25.0 25.5 26.0 26.5 27.0 27.5 28.0 28.5 29.0 29.5
[31] 30.0
length(distances)
[1] 31

Part 3: rep() — Repeating Values

Sometimes you need to repeat the same value multiple times — like recording that a player played in 4 quarters, or that a team has 5 starters.

# An NBA team has 5 starting players
# Record the starting designation for each player
starter_status <- rep("Starter", times = 5)
starter_status
[1] "Starter" "Starter" "Starter" "Starter" "Starter"
# A full roster has 15 players (5 starters + 10 bench)
bench_status <- rep("Bench", times = 10)
bench_status
 [1] "Bench" "Bench" "Bench" "Bench" "Bench" "Bench" "Bench" "Bench" "Bench"
[10] "Bench"
# Repeat a sequence of values
# 4 quarters, repeated for 3 overtime periods... wait, let's do it right:
# Game periods: Q1, Q2, Q3, Q4
quarters <- rep(1:4, times = 1)
quarters
[1] 1 2 3 4
# What if each quarter is repeated to show multiple games?
quarter_labels <- rep(1:4, times = 5)   # 5 games
quarter_labels
 [1] 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4

rep() with each =

# 3 players, each listed twice (home game and away game)
players <- rep(c("LeBron", "Curry", "Durant"), each = 2)
players
[1] "LeBron" "LeBron" "Curry"  "Curry"  "Durant" "Durant"
Tiptimes vs each
  • rep(x, times = 3) repeats the whole thing 3 times: 1 2 3 1 2 3 1 2 3
  • rep(x, each = 3) repeats each element 3 times: 1 1 1 2 2 2 3 3 3
# Demonstrate the difference
rep(1:3, times = 3)
[1] 1 2 3 1 2 3 1 2 3
rep(1:3, each = 3)
[1] 1 1 1 2 2 2 3 3 3

Part 4: Combining with c()

c() stands for combine (or concatenate). It’s how you stick multiple values or sequences together into one.

# Stephen Curry's points per game in his last 8 games
curry_points <- c(28, 32, 19, 41, 25, 30, 22, 38)
curry_points
[1] 28 32 19 41 25 30 22 38
# You can combine sequences too
first_half  <- 1:41
second_half <- 42:82
full_season <- c(first_half, second_half)
length(full_season)
[1] 82
# Mix of types? R will convert everything to the most flexible type
mixed <- c(10, 20, "thirty")
mixed          # Notice the numbers got converted to text!
[1] "10"     "20"     "thirty"
class(mixed)
[1] "character"
WarningType Coercion

When you mix types in c(), R automatically converts everything to the most general type. Numbers become text if there’s any text in the mix. This is called type coercion and can cause unexpected results. Keep your vectors to one type when possible.


Part 5: Math Across a Whole Sequence

One of R’s superpowers: you can do math on every element at once without writing a loop.

# LeBron James' points scored in 10 consecutive games
lebron_points <- c(26, 30, 19, 35, 28, 22, 31, 27, 33, 25)

# How many total points did he score?
sum(lebron_points)
[1] 276
# What was his average?
mean(lebron_points)
[1] 27.6
# Fantasy basketball often doubles points for star players
# Multiply every value by 2 at once!
fantasy_points <- lebron_points * 2
fantasy_points
 [1] 52 60 38 70 56 44 62 54 66 50
# Which games did he score 30 or more?
lebron_points >= 30
 [1] FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE  TRUE FALSE
# How many games did he score 30+?
sum(lebron_points >= 30)   # TRUE counts as 1, FALSE as 0
[1] 4
NoteVectorized Operations

In R, most operations automatically apply to every element in a sequence. This is called vectorization and it’s what makes R so efficient — no need to write loops for basic math.

lebron_points * 2 is equivalent to doing 26*2, 30*2, 19*2, ... all at once.


Part 6: Practical Example — An NBA Season Simulator

Let’s put it all together. We’ll build a simple season summary for the Golden State Warriors:

# Game numbers for the full 82-game season
games <- 1:82

# Simulate points scored per game (we'll use real-ish averages)
# For now, let's create a simple pattern using sequences
# (In later labs we'll use real data!)

# Warriors tend to score between 110 and 130 points
# Let's create a realistic-looking sequence (not truly random yet)
set.seed(42)   # Makes our "random" numbers reproducible
warriors_points <- round(seq(110, 130, length.out = 82) +
                        rep(c(-8, 5, -3, 10, -6, 8, -4), length.out = 82))

# Season summary
cat("=== Golden State Warriors Season Summary ===\n")
=== Golden State Warriors Season Summary ===
cat("Total games:", length(games), "\n")
Total games: 82 
cat("Average points per game:", round(mean(warriors_points), 1), "\n")
Average points per game: 120.2 
cat("Highest scoring game:", max(warriors_points), "points\n")
Highest scoring game: 140 points
cat("Lowest scoring game:", min(warriors_points), "points\n")
Lowest scoring game: 102 points
cat("Games scoring 120+:", sum(warriors_points >= 120), "\n")
Games scoring 120+: 44 
# Simple plot of points across the season
plot(games, warriors_points,
     type = "l",
     col  = "#1D428A",   # Warriors blue
     lwd  = 2,
     xlab = "Game Number",
     ylab = "Points Scored",
     main = "Golden State Warriors — Points Per Game\n2025–26 Season (simulated)")
abline(h = mean(warriors_points), col = "#FFC72C", lwd = 2, lty = 2)
legend("topright", legend = "Season Average", col = "#FFC72C", lty = 2, lwd = 2)

TipYour First Plot!

Don’t worry about understanding all the plot() arguments yet — we have a whole week dedicated to visualization. For now, just appreciate that R can draw a graph of your data in one function call.


3-Minute Knowledge Check

Close your notes. Answer these on your own — you have 3 minutes. We’ll go through the answers together.

CautionKnowledge Check Questions

1. What does 5:9 produce in R?

a) The number 59  &nbsp;&nbsp; b) `5 6 7 8 9`  &nbsp;&nbsp; c) `5 9`  &nbsp;&nbsp; d) An error

2. Which seq() argument controls how many values are generated?

a) `from`  &nbsp;&nbsp; b) `to`  &nbsp;&nbsp; c) `by`  &nbsp;&nbsp; d) `length.out`

3. What is the output of rep(c(1, 2), times = 3)?

a) `1 1 1 2 2 2`  &nbsp;&nbsp; b) `1 2 1 2 1 2`  &nbsp;&nbsp; c) `1 2 3 1 2 3`  &nbsp;&nbsp; d) `1 2`

4. What does length(1:82) return?

5. True or False: sum(c(TRUE, FALSE, TRUE, TRUE)) returns 3.

1. b) 5 6 7 8 9 — The : operator creates a sequence of consecutive integers from the first number to the second, inclusive.

2. d) length.out — This argument specifies the exact number of evenly spaced values to generate between from and to.

3. b) 1 2 1 2 1 2times = 3 repeats the whole vector 3 times. If you wanted 1 1 1 2 2 2, you would use each = 3 instead.

4. 821:82 creates a vector of 82 integers, and length() counts the number of elements.

5. True — In R, TRUE is treated as 1 and FALSE as 0 in arithmetic. So sum(TRUE, FALSE, TRUE, TRUE) = 1 + 0 + 1 + 1 = 3.


Lab 3 Checklist

Before you leave, make sure you can:

TipBonus Challenge

Create a sequence representing your favorite NBA player’s jersey numbers worn throughout their career (look it up!). Use rep() to show how many seasons they wore each number. Calculate how many total seasons are represented.


Before Next Class (Wednesday)

  • Review seq(), rep(), and c() — write three examples of each in your notes
  • Read Intro2r §2.4.1–2.4.5
  • Wednesday (Lab 4) we build on this with vectors and missing values