List all Objects in R

All entities in R are called objects. They can be arrays, numbers, strings, functions. In this tutorial we look at how to list all objects in R.

Get a list of all objects

function name :
objects()
or
ls()

the objects() or ls() function can be used to get a vector of character strings of the names of all objects in the environment.

The names in the result are sorted.

Listing object from a specific environment

By default the objects returned are from the environment from which ls() or objects() is called. It is possible to specify a different environment name. For example, if called within a function the objects returned are from the function scope. However it is possible to get all object in the global scope by using the .GlobalEnv environment.

function f1 creates an object a1. the ls function returns that object

Function f2 creates an object but lists all objects from the global scope

the objects a,d,z were created earlier and f1,f2 are functions. Since we have specified global scope, calling function f2 returns not only objects from the function scope but also objects from the global scope.

Listing objects that satisfy a particular pattern

It is also possible to list objects that specify a particular regular expression

The code below returns all objects whose name contains an f

The code below returns all objects whose name ends with “1”

This completes our tutorial on listing Objects in R. In the next tutorial we will see how to run R code contained in a file.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.