R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
Managing objects in R's memory is essential for keeping your R workspace organized and avoiding unnecessary memory consumption. Here's a tutorial on creating, listing, and deleting objects in R:
Objects in R can be variables, data frames, vectors, lists, functions, etc.
x <- 10 y <- "Hello, R!" z <- TRUE
numeric_vector <- c(1, 2, 3, 4, 5) character_vector <- c("apple", "banana", "cherry")
Using the built-in mtcars
dataset as an example:
data(mtcars) my_df <- mtcars[1:10,]
my_list <- list(name="John", age=25, scores=c(90, 85, 88))
To see all the objects currently in your R workspace:
ls()
This will return a character vector of object names.
If you want more details about the objects, such as their type and size, you can use:
ls.str()
To delete a single object:
rm(x) # This will remove the variable 'x' from the memory.
To remove multiple objects at once:
rm(y, z) # This will remove both 'y' and 'z'.
To clear your entire workspace:
rm(list=ls())
This is useful if you want to start fresh or if you have a large number of objects that you no longer need.
It's sometimes useful to check how much memory your R objects are consuming, especially with large datasets.
To get an overview of memory usage:
object.size(my_df) # Returns the size of 'my_df' in bytes.
You can save one or multiple objects to a file:
save(my_list, my_df, file="my_data.RData")
To load saved objects back into your R workspace:
load("my_data.RData")
Effectively managing objects in R's memory is crucial, especially when working on complex projects or dealing with large datasets. Regularly cleaning up unnecessary objects, saving important objects to files, and monitoring memory usage can help keep your R sessions efficient and organized.
Assigning variables and objects in R programming:
# Assigning a variable x <- 5 # Creating a vector my_vector <- c(1, 2, 3, 4, 5) # Creating a data frame my_df <- data.frame(Name = c("John", "Alice"), Age = c(25, 30))
Listing objects in R workspace:
# Listing objects in the workspace ls()
Viewing and exploring objects in R:
# Viewing the contents of an object print(my_vector) # Exploring structure of an object str(my_df) # Summary statistics of an object summary(my_vector)
Deleting objects from memory in R:
# Deleting a single object rm(x) # Deleting multiple objects rm(my_vector, my_df)
Clearing R workspace and memory:
# Clearing the entire workspace rm(list = ls()) # Alternatively, restart R session to clear all objects and memory
R ls()
function for listing objects:
# Using ls() to list objects ls()
ls()
with patterns to filter objects:ls(pattern = "my_")