ls() in R: list objects/variables in your environment

Often while working in R, you might want to check your environments for all the objects you have created and available in your environment. ls() function in R lists the objects in your environment as a vector. In this tutorial, we will learn the use of ls() functions with examples. In addition to ls() function, we will also learn about two related functions objects() and ls.str() and see examples of using them.

list objects in environment with ls() in R

Let us assume that we have started a new R session and create a new variable x

x <- "Hello"

By using ls() function we can see that our current environment has a single object x

ls()

## [1] "x"

By creating couple of more variables and then using ls() function, we can see that we have additional objects available in our current working enviornment.

y <- "Welcome to RStats101.com"
z <- "Learn Basics of R"

Note that the results of using ls() function is a vector containing the names of the variables or objects in the working enviornment.

ls()
## [1] "x" "y" "z"

list hidden objects in environment with ls() in R

By default ls() function ignores listing objects or variables whose name begin with a dot “.”. Let us create a new variable that begins with a “.”

.dotted <- hidden()

Note that using ls() function, we are not seeing the object name in the resulting vector.

ls()
## [1] "x" "y" "z"

Typically, variable names that begin with a dot are considered hidden. By using the “all.names=TRUE” argument within ls() function we can see the variables that has dot at the start.

ls(all.names = TRUE)
## [1] ".dotted" "x"       "y"       "z"

list variables in environment with obejcts() in R

Another related function available in R is objects(). objects() function behave very similarly. In this example below we can see that, using objects() function gives us the same results as ls() function.

objects()
## [1] "x" "y" "z"

list variables in environment and structure with ls.str() in R

ls.str() function is another useful function that lists the variables in your current environment with additional details. ls.str() function kind of combines the functionality of str() function in R that helps to look at the structure of an object in R with ls() function.

For example, if apply ls.str() function to the current environment, we can see the object names and its values.

ls.str()
## x :  chr "Hello"
## y :  chr "Welcome to RStats101.com"
## z :  chr "Learn Basics of R"

Let us add a new variable to the environment. This time we add a dataframe, instead of a simple object.

df <- mtcars

As we saw before, ls() function simply returns the names of variables in the environment as a vector.

ls()
## [1] "df" "x"  "y"  "z"

However, ls.str() function gives us more details by providing structure of each object. In the case of a dataframe, it gives us variable names in the dataframe, their types and their first few values.

ls.str()
## df : 'data.frame':   32 obs. of  11 variables:
##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ disp: num  160 160 108 258 360 ...
##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ qsec: num  16.5 17 18.6 19.4 17 ...
##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
## x :  chr "Hello"
## y :  chr "Welcome to RStats101.com"
## z :  chr "Learn Basics of R"

Note that ls.str() also ignored variable names that begins with a dot. We need to use all.names=TRUE argument to see the hidden variables.

ls.str(all.names=TRUE)
## .dotted :  chr "hidden"
## df : 'data.frame':   32 obs. of  11 variables:
##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ disp: num  160 160 108 258 360 ...
##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ qsec: num  16.5 17 18.6 19.4 17 ...
##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...
## x :  chr "Hello"
## y :  chr "Welcome to RStats101.com"
## z :  chr "Learn Basics of R"

To summarise, in this tutorial we saw how to identify the objects/variables in your current working environment in R using ls() function. One of the common next steps after identifying object/variables is to remove them. Tune in for another tutorial to remove one or multiple objects in your environment.

Exit mobile version