Introduction to Objects in R
In R, everything is an object. This is one of the most important concepts to understand when learning R programming. Variables, data structures, functions, models, and even expressions are treated as objects stored in memory.
An object in R is a named container that holds data, attributes, and sometimes behavior.
Examples of objects:
- Numeric values
- Vectors
- Matrices
- Data frames
- Lists
- Functions
- Models (like linear regression results)
What is an Object?
An object is a data entity stored in memory that has:
- A value (data)
- A type (numeric, character, list, etc.)
- Attributes (optional metadata)
- A class (used in OOP)
Example:
x <- 10
Here:
xis an object10is the value- Type is numeric
- Class is numeric
Everything in R is an Object
In R:
- Numbers are objects
- Strings are objects
- Functions are objects
- Data frames are objects
Example:
x <- 5
y <- "R"
f <- function(a) a + 1
Check their classes:
class(x)
class(y)
class(f)
Creating Objects in R
Objects are created using the assignment operator <- (preferred) or =.
a <- 100
b = "Data Science"
Each assignment creates an object in memory.
Naming Objects
Object names follow the same rules as variable names.
Rules:
- Must start with a letter or
. - Cannot start with a number
- Case-sensitive
- Cannot be reserved keywords
Valid:
score <- 95
.myObject <- 10
Invalid:
1score <- 95
Types of Objects in R
Objects in R can be categorized based on the data they store.
Atomic Objects
These store a single type of data.
- Numeric
- Integer
- Character
- Logical
- Complex
Example:
x <- 10
y <- TRUE
z <- "R"
Composite Objects
These store multiple values and structures.
| Object Type | Description |
|---|---|
| Vector | 1D, same data type |
| Matrix | 2D vector |
| Array | Multi-dimensional |
| List | Different data types |
| Data Frame | Tabular data |
Example:
v <- c(1, 2, 3)
m <- matrix(1:6, nrow = 2)
l <- list(1, "R", TRUE)
df <- data.frame(id=1:3, name=c("A","B","C"))
Object Attributes
Attributes provide additional information about an object.
Common attributes:
namesdimclasslevels
Example:
v <- c(a=10, b=20)
attributes(v)
Accessing Attributes
Using attributes()
attributes(v)
Using attr()
attr(v, "names")
Modifying Attributes
attr(v, "comment") <- "Score values"
Class of an Object
The class determines how an object behaves in functions.
class(v)
Change class manually:
class(v) <- "myVector"
This is commonly used in S3 OOP.
Object Structure – str()
The str() function displays the internal structure of an object.
str(df)
Output shows:
- Type
- Length
- Attributes
- Data preview
Checking Object Properties
Type of Object
typeof(x)
Class of Object
class(x)
Length of Object
length(v)
Dimensions of Object
dim(m)
Copying Objects in R
R uses copy-on-modify behavior.
x <- c(1,2,3)
y <- x
y[1] <- 100
x remains unchanged until modification.
Reference Objects (R6)
Some objects (like R6) use reference semantics.
library(R6)
Counter <- R6Class("Counter",
public = list(
value = 0,
inc = function() self$value <- self$value + 1
)
)
c1 <- Counter$new()
c2 <- c1
c2$inc()
c1$value # changed
Listing Objects in Memory
Use ls() to see objects in the current environment.
ls()
Removing Objects from Memory
Use rm() to delete objects.
rm(x)
Remove multiple objects:
rm(a, b, c)
Remove all objects:
rm(list = ls())
Object Environments
Objects live inside environments.
Common environments:
- Global Environment
- Local Environment
- Package Environment
Check current environment:
environment()
Objects and Functions
Functions are also objects.
f <- function(x) x * 2
class(f)
Functions can be:
- Passed as arguments
- Returned from other functions
- Stored in lists
Looping Over Objects
Example with vector:
v <- c(10, 20, 30)
for (i in v) {
print(i)
}
Example with list:
l <- list(1, "R", TRUE)
for (item in l) {
print(item)
}
Practical Example
student <- list(
name = "Alice",
marks = c(80, 85, 90)
)
average <- mean(student$marks)
average
Common Mistakes with Objects
- Confusing type and class
- Modifying reference objects unintentionally
- Forgetting objects are copied on modification
- Removing important objects accidentally using
rm()
Summary
- Everything in R is an object
- Objects store data, attributes, and class
- Objects live in environments
- Understanding objects is essential for OOP, data analysis, and package development
- Proper object management improves performance and code clarity
Leave a Reply