R Objects
In R, everything is an object. Data, functions, models, and even results of calculations are all stored as objects. Understanding R objects is fundamental to working with data, writing reproducible code, and performing statistical analyses.
This article introduces what R objects are, common object types, how to create them, and how to inspect them.
An R object is anything that R can store in memory and work with. When you assign a value to a name, you are creating an object.
# store 10 in x
x <- 10
10 is the value
x is the object name
<- is the assignment operator
x + 5
Object names in R:
Can contain letters, numbers, . and _
Must start with a letter or .
Are case-sensitive (Data ≠ data)
student_scores
mean_value
A vector is a one-dimensional collection of values of the same type.
scores <- c(85, 90, 88, 92)
Common vector types:
Numeric: c(1, 2.5, 3)
Character: c("A", "B", "C")
Logical: c(TRUE, FALSE, TRUE)
A data frame is a table-like object where:
Columns can be different types
Each column is a vector
Each row represents an observation
students <- data.frame(
name = c("Alex", "Jordan", "Taylor"),
score = c(85, 90, 88),
passed = c(TRUE, TRUE, TRUE)
)
A matrix is a two-dimensional object where all values must be the same type.
M <- matrix(c(1, 2, 3, 4), nrow = 2)
Matrices are often used in linear algebra and statistical modeling.
A list can store different types of objects together.
my_list <- list(
numbers = c(1, 2, 3),
text = "hello",
flag = TRUE
)
Lists are flexible and commonly used to store model outputs.
Functions are also objects in R.
square <- function(x) {
x^2
}
You can store, pass, and modify functions like any other object.
Students
class(students)
str(students)
summary(students)
These commands help you understand what kind of data an object contains and how it is organized.
Objects can be overwritten or updated:
x <- 5
x <- x + 1
You can also modify parts of objects:
students$score <- students$score + 5
To remove an object from memory:
rm(x)
To see all objects currently in your environment:
ls()
Understanding R objects allows you to:
Store and manipulate data efficiently
Understand outputs from statistical models
Debug errors by checking object structure
Write clear and reproducible code
Most errors in beginner R programming occur because an object is not the expected type or structure.
Everything in R is an object
Objects store data, functions, and results
Common objects include vectors, data frames, matrices, lists, and functions
Use class(), str(), and summary() to inspect objects
Mastery of R objects is essential for statistics and data science workflows