R Objects

R Objects

R Objects

Overview

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.

What Is an R Object?

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


Here:
  • 10 is the value

  • x is the object name

  • <- is the assignment operator


# You can now use x in calculations

x + 5


Object Names

Object names in R:

  • Can contain letters, numbers, . and _

  • Must start with a letter or .

  • Are case-sensitive (Datadata)


Examples:


age

student_scores

mean_value


Avoid using names that overwrite built-in functions, such as mean, sum, or data.

Common Types of R Objects

1. Vectors

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)

2. Data Frames

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)

)

3. Matrices

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.

4. Lists

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.

5. Functions

Functions are also objects in R.

square <- function(x) {

  x^2

}

You can store, pass, and modify functions like any other object.

Inspecting R Objects

Viewing an Object

Students

Checking the Object Type

class(students)

Viewing Structure

str(students)

Getting a Summary

summary(students)

These commands help you understand what kind of data an object contains and how it is organized.

Modifying Objects

Objects can be overwritten or updated:

x <- 5

x <- x + 1

You can also modify parts of objects:

students$score <- students$score + 5

Removing Objects

To remove an object from memory:

rm(x)

To see all objects currently in your environment:

ls()

Why R Objects Matter in Data Science

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.

Key Takeaways

  • 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

    • Related Articles

    • R Function

      An R function is a reusable block of code in the R programming language that performs a specific task. Functions accept inputs (called arguments), execute a set of instructions, and return an output. They are fundamental to writing efficient, ...
    • Objects (in Research)

      Objects (in Research) In research, objects—also called cases or research units—are the entities that a study focuses on. They are the “things” about which data are collected and analyzed. Depending on the discipline and research question, objects may ...
    • factor (in R)

      Factor (in R) is a type of R object that is a categorical variable.
    • numeric (in R)

      Numeric (in R) is a type of R object that is a quantitative variable.
    • Using the R Sandbox

      The R Sandbox The R Sandbox is a separate R window you can use to experiment with R code and play around with ideas from the course. It comes pre-loaded with the base R packages and all of the packages and datasets we reference in the textbook. ...