Creating, Listing, and Deleting Objects in Memory with Example
In R, what is often referred to as an “object” is equivalent to what many other programming languages call a “variable.” Objects and variables, depending on the context, can have very different interpretations. Variables in all programming languages provide a way to access data stored in memory. However, R does not offer direct access to memory; instead, it provides various specialized data structures, referred to as objects.
In R, objects are accessed through symbols or variable names. Interestingly, symbols in R are themselves objects and can be manipulated like other objects. This unique feature of R has significant implications. In R, everything, from numbers to strings, arrays, vectors, lists, and data frames, is treated as an object.
Creating Objects in Memory
To perform operations in R, you must assign values to objects. This is done by giving the object a name, followed by an assignment operator (<- or =), and then the desired value.
Example:
# Assigning values to objects
num <- 10
text <- "Hello, R!"
vec <- c(4, 8, 12)
Here, the <- symbol is an assignment operator. For instance, after executing num <- 10, the value 10 is assigned to the object num. The assignment can be read as “10 is assigned to num.”
variable_name <- list(attribute1, attribute2, ..., attributeN)
Rules for Naming Objects
- Object names can be any combination of letters, numbers, and underscores, like
age,user_name, orid_number. - Names cannot start with a number (e.g.,
3varis invalid, butvar3is valid). - R is case-sensitive (
Dataanddataare different objects). - Avoid using reserved keywords or function names (e.g.,
if,for,data). - Refrain from using dots in object names, as they have special meanings in R. Use underscores (
_) instead. - Prefer descriptive names for better readability. For example, use
student_ageinstead ofsa. - Be consistent in naming conventions and coding styles.
Creating Objects: Examples
# Numeric object
num <- 10
print(num)
# Character object
message <- "Learning R is fun!"
print(message)
# Vector object
num_vec <- c(2, 4, 6)
print(num_vec)
char_vec <- c("Red", "Green", "Blue")
print(char_vec)
# List object
data_list <- list(
"Numbers" = num_vec,
"Colors" = char_vec
)
print(data_list)
# Data frame object
data_frame <- data.frame(
"ID" = 1:3,
"Name" = c("Alice", "Bob", "Carol")
)
print(data_frame)
Output:
[1] 10
[1] "Learning R is fun!"
[1] 2 4 6
[1] "Red" "Green" "Blue"
$Numbers
[1] 2 4 6
$Colors
[1] "Red" "Green" "Blue"
ID Name
1 1 Alice
2 2 Bob
3 3 Carol
Listing Objects in Memory
To list all objects in the current workspace, use the ls() or objects() function. Both return the names of all objects in memory as character vectors.
Example:
# Create objects
num <- 10
message <- "Hello"
vec <- c(1, 2, 3)
# List objects
print(ls())
print(objects())
Output:
[1] "message" "num" "vec"
[1] "message" "num" "vec"
Listing Objects Matching a Pattern
You can also use ls() with a pattern to filter objects by name.
# Create objects
val1 <- 5
value2 <- 10
test_var <- "Sample"
# List objects containing 'val'
print(ls(pattern = "val"))
# List objects ending with 'e'
print(ls(pattern = "e$"))
Output:
[1] "val1" "value2"
[1] "test_var"
Deleting Objects in Memory
To remove objects from the workspace, use the rm() or remove() function. Deleting objects helps free up memory and declutter the environment.
Example:
# Create objects
x <- 25
name <- "Sample Name"
vec <- c(1, 2, 3)
# Delete a specific object
rm(x)
# Delete another object
remove(name)
# List remaining objects
print(ls())
Output:
[1] "vec"
Deleting All Objects
To delete all objects in memory, use rm(list = ls()).
# Create objects
a <- 15
b <- "Hello"
# Remove all objects
rm(list = ls())
# List objects
print(ls())
Output:
character(0)
Leave a Reply