RObjects (original) (raw)

R-Objects

Last Updated : 12 Jul, 2025

In R programming, objects are the fundamental data structures used to store and manipulate data. Objects in R can hold different types of data, such as numbers, characters, lists, or even more complex structures like data frames and matrices.

An object in R is important an instance of a class and can be assigned to a variable. Unlike many other programming languages, R does not require variables to be explicitly declared with a data type. Instead, variables in R are associated with objects and R automatically determines the object type based on the assigned value.

Key Points

  1. **Dynamic Typing: R automatically infers the type of an object based on the data assigned to it.
  2. **Types of Objects: R supports various objects like vectors, lists, data frames, matrices and functions.
  3. **Memory Allocation: Unlike many languages, where memory is allocated based on explicit data types, R assigns memory dynamically when objects are created or modified.
  4. **Manipulating Objects: Once an object is created, it can be manipulated using various built-in functions and the type of the object can be checked using functions like class(), typeof() and **mode().

Type of Objects

There are 5 basic types of objects in the R language:

1. Vector

Vectors are one of the basic types of objects in R programming. Atomic vectors can store homogeneous data types such as character, doubles, integers, raw, logical and complex. A single element variable is also said to be vector.

**Example:

R `

x <- c(1, 2, 3, 4) y <- c("a", "b", "c", "d") z <- 5 a <- c("Jayan will be",25,"years old in",2028)

cat(x,":" ,class(x),"\n") cat(y,":" ,class(y),"\n") cat(z,":" ,class(z),"\n") cat(a,":" ,class(a),"\n")

`

**Output:

vec

Vectors

2. List

List is another type of object in R programming. List can contain heterogeneous data types such as vectors or another lists.

**Example:

Python `

ls1 <- list(c(1, 2, 3, 4)) ls2 <- list("a", "b", "c") ls3 <- c(ls1,ls2)

ls class(ls) print("------------------------------") ls2 class(ls2) print("------------------------------") ls3 class(ls3)

`

**Output:

list

Lists

3. Matrix

Matrix is used to store values as 2-Dimensional array, matrices are used in R. Data, number of rows and columns are defined in the matrix() function.

**Syntax:

matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL)

**Example:

R `

x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)

mat <- matrix(x, nrow = 3)

print(mat) class(mat)

`

**Output:

matrix

Matrix

4. Factor

Factor object encodes a vector of unique elements (levels) from the given data vector.

**Example:

R `

s <- c("spring", "autumn", "winter", "summer","spring", "autumn")

factor(s)

print(nlevels(factor(s)))

`

**Output:

factor

Factors

5. Array

An array is a data structure that stores elements of the same type in a fixed-size, indexed collection. array() function is used to create n-dimensional array.

**Syntax:

array(data, dim = length(data), dimnames = NULL)

**Example:

R `

arr <- array(c(1, 2, 3), dim = c(3, 3, 3))

print(arr)

`

**Output:

arr

Arrays

6. Data Frame

Data frame is 2-dimensional tabular data object in R programming. It consists of multiple columns and each column represents a vector. Columns in data frame can have different modes of data unlike matrices.

**Example:

R `

x <- 1:10 y <- LETTERS[1:10] z <- c("Ansh", "Raj", "Sahil", "Vikram", "Ravi", "Kareena", "Anita", "Jayan", "Priya", "Sunil")

df <- data.frame(x, y, z)

df

`

**Output:

dataframe

Data Frame

In this article, we explored various types of objects in R programming, including vectors, lists, matrices, factors, arrays, and data frames, each with their own unique properties and uses for data storage and manipulation.