Open In App

R – Creating, Listing, and Deleting Objects in Memory

Improve
Improve
Like Article
Like
Save
Share
Report

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)


Last Updated : 29 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads