R - Creating, Listing, and Deleting Objects in Memory
Last Updated :
29 Mar, 2023
One of the most interesting facts about R is, what is known as objects in R are known as variables in many other programming languages. Depending on the context objects and variables can have drastically different meanings. In every computer language variables provide a means of accessing the data stored in memory. R does not provide direct access to the computer’s memory but rather provides several specialized data structures we will refer to as objects. These objects are referred to through symbols or variables. In R, however, the symbols are themselves objects and can be manipulated in the same way as any other object. This is different from many other languages and has wide-ranging effects. All entities in R can be called as objects. They can be arrays, numbers, strings, lists, vectors, data frames, etc.
Creating Object in Memory
To do useful and interesting stuff in R, one needs to assign values to objects. To create an object in R, one needs to give it a name followed by the assignment operator <- (An equal sign, =, can also be used), and the value he wants to give it:
x <- 5
<- is the assignment operator. It assigns values on the right to objects on the left. So, after executing x <- 5, the value of x is 5. The arrow can be read as 5 goes into x.
Naming rules for objects:
- Objects can be given any name such as x, personName, or subject_id.
- Make the object names to be explicit and not too long.
- Objects name can not start with a number (2x is not valid, but x2 is).
- R is case sensitive (e.g., subject_id is different from Subject_id).
- There are some names that cannot be used because they are the names of fundamental functions in R (e.g., if, else, for, see Reserved words in R for a complete list).
- In general, even if it is allowed, it’s best to not use other function names (e.g., c, T, mean, data, df, weights, etc.).
- It’s also best to avoid dots (.) within a variable name as in my.dataframe. There are many functions in R with dots in their names for historical reasons, but because dots have a special meaning in R (for methods) and other programming languages, it is best to avoid them.
- It is also recommended to use nouns for variable names, and verbs for function names.
- It’s important to be consistent in the styling of one's code (where he puts spaces, how he names variables, etc.). Using a consistent coding style makes one's code clearer to read for his future self and his collaborators.
Example:
R
# R program to illustrate
# Creating an object in memory
# Numerical object
x = 5
print(x)
# String object
name = "Amiya"
print(name)
# Vector object
vec1 = c(1, 2, 3)
print(vec1)
vec2 = c("A", "B", "C")
print(vec2)
# List object
listOfNumber = list(
"Numbers" = vec1,
"Characters" = vec2
)
print(listOfNumber)
# Data frame object
myDataFrame = data.frame(
"Numbers" = vec1,
"Characters" = vec2
)
print(myDataFrame)
Output:
[1] 5
[1] "Amiya"
[1] 1 2 3
[1] "A" "B" "C"
$Numbers
[1] 1 2 3
$Characters
[1] "A" "B" "C"
Numbers Characters
1 1 A
2 2 B
3 3 C
Listing Object in Memory
One can list all the objects in his working directory by using objects() or ls() function. objects() or ls() function can be used to get a vector of character strings of the names of all objects in the environment.
Example:
R
# R program to illustrate
# Listing objects in memory
# Numerical object
x = 5
print(x)
# String object
name = "Amiya"
print(name)
# Vector object
vec1 = c(1, 2, 3)
print(vec1)
vec2 = c("A", "B", "C")
print(vec2)
# List object
listOfNumber = list(
"Numbers" = vec1,
"Characters" = vec2
)
print(listOfNumber)
# Data frame object
myDataFrame = data.frame(
"Numbers" = vec1,
"Characters" = vec2
)
print(myDataFrame)
# Listing objects using object()
cat("Using object()\n")
print(objects())
# Listing objects using ls()
cat("Using ls()\n")
print(ls())
Output:
[1] 5
[1] "Amiya"
[1] 1 2 3
[1] "A" "B" "C"
$Numbers
[1] 1 2 3
$Characters
[1] "A" "B" "C"
Numbers Characters
1 1 A
2 2 B
3 3 C
Using object()
[1] "listOfNumber" "myDataFrame" "name" "vec1" "vec2"
[6] "x"
Using ls()
[1] "listOfNumber" "myDataFrame" "name" "vec1" "vec2"
[6] "x"
One may notice that the objects() or ls() function returns the result in a sorted order.
Listing objects that satisfy a particular pattern: In R it is also possible to list objects that specify a particular regular expression.
Example:
R
# R program to illustrate
# Listing objects in memory
# Numerical object
x = 5
print(x)
# String object
name = "Amiya"
print(name)
# Vector object
vec1 = c(1, 2, 3)
print(vec1)
vec2 = c("A", "B", "C")
print(vec2)
# List object
listOfNumber = list(
"Numbers" = vec1,
"Characters" = vec2
)
print(listOfNumber)
# Data frame object
myDataFrame = data.frame(
"Numbers" = vec1,
"Characters" = vec2
)
print(myDataFrame)
# The code below returns all objects
# whose name contains an v
print(ls(pattern = "v"))
# The code below returns all objects
# whose name ends with "e"
print(ls(pattern = "e$"))
Output:
[1] 5
[1] "Amiya"
[1] 1 2 3
[1] "A" "B" "C"
$Numbers
[1] 1 2 3
$Characters
[1] "A" "B" "C"
Numbers Characters
1 1 A
2 2 B
3 3 C
[1] "vec1" "vec2"
[1] "myDataFrame" "name"
Deleting Object in Memory
One can delete the objects in his working directory by using rm() or remove() function. rm() or remove() function can be used to free up the memory and clear the environment space.
Example:
R
# R program to illustrate
# Deleting objects in memory
# Numerical object
x = 5
print(x)
# String object
name = "Amiya"
print(name)
# Vector object
vec1 = c(1, 2, 3)
print(vec1)
vec2 = c("A", "B", "C")
print(vec2)
# List object
listOfNumber = list(
"Numbers" = vec1,
"Characters" = vec2
)
print(listOfNumber)
# Data frame object
myDataFrame = data.frame(
"Numbers" = vec1,
"Characters" = vec2
)
print(myDataFrame)
# Deleting object x using rm()
rm(x)
# Deleting object myDataFrame using remove()
remove(myDataFrame)
cat("After deleted following objects listing of the object:\n")
print(ls())
Output:
[1] 5
[1] "Amiya"
[1] 1 2 3
[1] "A" "B" "C"
$Numbers
[1] 1 2 3
$Characters
[1] "A" "B" "C"
Numbers Characters
1 1 A
2 2 B
3 3 C
After deleted following objects listing of the object:
[1] "listOfNumber" "name" "vec1" "vec2"
Deleting All Objects in Memory
One can also delete all objects inside the memory by just passing an argument list = ls() to the function rm() or remove().
Example:
R
# R program to illustrate
# Deleting objects in memory
# Numerical object
x = 5
print(x)
# String object
name = "Amiya"
print(name)
# Vector object
vec1 = c(1, 2, 3)
print(vec1)
vec2 = c("A", "B", "C")
print(vec2)
# List object
listOfNumber = list(
"Numbers" = vec1,
"Characters" = vec2
)
print(listOfNumber)
# Data frame object
myDataFrame = data.frame(
"Numbers" = vec1,
"Characters" = vec2
)
print(myDataFrame)
# Deleting all objects using rm()
rm(list = ls())
cat("After deleted all objects listing of the object:\n")
print(ls())
Output:
[1] 5
[1] "Amiya"
[1] 1 2 3
[1] "A" "B" "C"
$Numbers
[1] 1 2 3
$Characters
[1] "A" "B" "C"
Numbers Characters
1 1 A
2 2 B
3 3 C
After deleted all objects listing of the object:
character(0)
Similar Reads
Determine Memory Usage of Data Objects in R
In this article, we are going to discuss how to find the memory used by the Data Objects in R Language. In R, after the creation of the object, It will allocate some space to the particular object. The object in memory is stored in bytes. Numeric type can allocate 56 bytes and character type can all
2 min read
R Variables - Creating, Naming and Using Variables in R
A variable is a memory allocated for the storage of specific data and the name associated with the variable is used to work around this reserved block. The name given to a variable is known as its variable name. Usually a single variable stores only the data belonging to a certain data type. The na
6 min read
Creation and Execution of R File in R Studio
R Studio is an integrated development environment(IDE) for R. IDE is a GUI, where you can write your quotes, see the results and also see the variables that are generated during the course of programming. R is available as an Open Source software for Client as well as Server Versions. Creating an R
5 min read
Create, Append and Modify List in R
In R Programming Language, the list is a one dimensional data structure which can hold multiple data type elements. In this article, we are going to create a list and append data into the list and modify the list elements. Creating a list List can be created by using list() function. Syntax: list(va
3 min read
Deleting Memory in C
In C programming, we might allocate memory dynamically for various tasks but what happens when those pieces of memory are no longer needed? If not managed properly, they can lead to memory leaks, wasting valuable resources, and slowing down our program. Therefore, we need to manage memory in C by pr
5 min read
Memory Management in Objective-C
Memory management is a core concept irrespective of any programming language. The process of allocation of memory of objects when needed and deallocation when they are no longer in use is called Memory Management. Memory is a limited resource and so has to be dealt with carefully. If unrequired memo
7 min read
Getting attributes of Objects in R Language - attributes() and attr() Function
attribute() function in R Programming Language is used to get all the attributes of data. This function is also used to set new attributes to data. Syntax: attributes(x) Parameters:Â x: object whose attributes to be accessed.Getting attributes of Objects in RExample 1: Implementing attributes() func
2 min read
How to Create, Access, and Modify Vector Elements in R ?
In this article, we are going how to create, modify, and access vectors in vector elements in the R Programming Language. Vector is a one-dimensional data structure that holds multiple data type elements. Creating a vectorIt can be done in these ways: Using c() function.Using: operator.Using the seq
5 min read
Create a numeric vector in R
A one-dimensional array containing numerical data is called a numeric vector in R Programming Language. Numerical values, such as integers or real numbers, are often stored and manipulated using numerical vectors. Concepts related to the topicNumerical Data Types: Real and integer numbers may be rep
2 min read
How to create a list in R
In this article, we will discuss What is a list and various methods to create a list using R Programming Language. What is a list?A list is the one-dimensional heterogeneous data i.e., which stores the data of various types such as integers, float, strings, logical values, and characters. These list
2 min read