Objects, Vectors, Basic Types (Including Factors)

Understand how R stores data: objects, vectors, basic types, and categorical variables, with PMx-relevant examples.
Tip

Big idea: In R, everything is an object, and most objects you work with in PMx are vectors.

Learning Objectives

By the end of this lesson, you will be able to:

  • Explain what an object is in R.
  • Create and inspect vectors.
  • Recognize basic R data types (numeric, character, logical, factor).
  • Understand vector length and recycling.
  • Anticipate common PMx mistakes related to vector operations.

Objects in R

An object is a named container that stores a value.

dose <- 100
dose
[1] 100

Here:

  • dose is the object name
  • 100 is the value
  • <- assigns the value to the object
Note

You’ll see = used sometimes, but <- is the PMx and R-community standard.


Vectors: The Core Data Structure

In R, a vector is a collection of values of the same type.

times <- c(0.5, 1, 2, 4, 8)
times
[1] 0.5 1.0 2.0 4.0 8.0

Even a single number is a vector of length 1.

length(dose)
[1] 1

Basic Data Types

Numeric

conc <- c(1.2, 3.5, 2.8)
class(conc)
[1] "numeric"

Used for:

  • concentrations
  • times
  • doses
  • covariates like weight or age

Character

ids <- c("101", "102", "103")
class(ids)
[1] "character"

Often used for:

  • subject IDs
  • study labels
  • treatment names

Logical

blq_flag <- c(FALSE, FALSE, TRUE)
class(blq_flag)
[1] "logical"

Logical vectors are critical for:

  • QC rules
  • filtering
  • conditional logic

Factor (Categorical Variables)

A factor is a special type of vector used to represent categorical variables.

arm <- factor(c("A", "B", "A"))
class(arm)
[1] "factor"
levels(arm)
[1] "A" "B"

Factors are commonly used for:

  • treatment arms
  • sex
  • study groups
  • categorical covariates
Note

Factors are stored internally as integers with labeled levels.
This matters for modeling, contrasts, and regression later.

We will revisit factors in more depth when we discuss modeling and data frames.


Inspecting Objects

Key inspection tools:

length(times)
[1] 5
class(times)
[1] "numeric"
str(times)
 num [1:5] 0.5 1 2 4 8

These are your first-line QC checks.


Vectorized Operations (Why R Feels Different)

Operations apply element-wise.

times * 2
[1]  1  2  4  8 16
conc > 2
[1] FALSE  TRUE  TRUE

This is why R is powerful for PMx work.


Recycling Rules (Important and Dangerous)

When vectors have different lengths, R may recycle values.

c(1, 2, 3) + 10
[1] 11 12 13
c(1, 2, 3) + c(10, 20)
[1] 11 22 13
Warning

Recycling can silently introduce errors if you’re not careful.

Rule of thumb:

  • Recycling is safe only when intentional and obvious.
  • If lengths don’t match expectations, stop and check.

PMx Example: Dose Adjustment

dose <- c(100, 100, 100)
adjustment <- c(1, 0.8, 1)

dose * adjustment
[1] 100  80 100

This works because vectors align element-wise.


Missing Values (NA)

Missing values are represented by NA.

conc <- c(1.2, NA, 2.8)
mean(conc)
[1] NA
mean(conc, na.rm = TRUE)
[1] 2
Note

Never assume missing values are handled automatically.


Strategies

  • Check length() and class() early and often.
  • Be explicit when creating vectors.
  • Watch for warnings about recycling.
  • Treat NA handling as a design choice, not an afterthought.

Practice Problems

  1. Create a numeric vector of three doses.
  2. Multiply it by a single adjustment factor.
  3. Create a logical vector identifying doses greater than 80.
  4. Create a factor representing treatment arms.
  5. Check the class and length of each object.

doses <- c(50, 100, 150)

doses * 0.9
[1]  45  90 135
doses > 80
[1] FALSE  TRUE  TRUE
arm <- factor(c("A", "B", "A"))

class(doses)
[1] "numeric"
length(doses)
[1] 3

Summary

You now understand:

  • how objects and vectors work in R
  • the main data types used in PMx (numeric, character, logical, factor)
  • why vectorization matters
  • how recycling and missing values can trip you up

These concepts underpin every data manipulation and visualization step that follows.


  • Everything is a vector—even length 1 objects.
  • Factors are categorical vectors.
  • Vectorized code is clearer and safer than loops (most of the time).
  • Always check length and class before trusting results.
  • Be explicit about how missing values are handled.