In this tutorial, we will share with you the usage, the syntax, and how to use the ls()
function in R. The ls() function can help you list the objects in your R environment. Follow us to learn more about the ls() function with the explanation and examples below.
Ls() Function In R
What does the ls() function do in R?
The ls()
function in R helps you list the objects in your R environment. This function will return the vector of the character Strings, which are the names of the object in your R environment. You can list the specified objects by assigning the value to the parameters of the ls() function. Let’s take a look at the syntax of the ls() function in R.
Syntax
ls(name, pos, envir, all.names, pattern, sorted)
Parameters
- name: The name of the environment to be listed objects.
- pos: The alternative argument of the ‘name’ parameter.
- envir: Another alternative argument of the ‘name’ parameter.
- all.names: Logical. The default is FALSE. List the objects whose name starts with a dot or not.
- pattern: List the objects whose names match the specified pattern.
- sorted: Logical. The default is TRUE. If TRUE, the result as the vector of character Strings will be sorted alphabetically.
After learning the usage and the syntax of the ls() function in R, you will learn how to use it in the next title below.
How To Use The Ls() Function In R
The ls() function is used to list the specified objects in the specified R environment. You can customize the result of this function by changing the value of its parameters.
Use the ls() function to list all objects in R environment
You can use the ls() function to list all objects. Simply, you only need to use the default syntax of the ls() function.
Look at the example below.
# Create a vector vec <- c("LearnShareIT", 3, 4, 5) # Create a data frame df <- data.frame(festivals = c("Halloween", "Christmas")) # Create a character String .hidden <- "LearnShareIT" # List all objects ls()
Output
[1] "df" "vec"
As you can see, the ‘.hidden’ object is not listed. This problem happens because the ls() function does not list the object whose name starts with a dot.
List all objects even if its name start with a dot
As you can see in the previous title, the ‘.hidden’ object is not listed. This problem happens because the ls() function does not list the object whose name starts with a dot. To handle that, you must assign the value TRUE to the ‘all.names’ parameter.
Look at the example below.
# Create a vector vec <- c("LearnShareIT", 3, 4, 5) # Create a data frame df <- data.frame(festivals = c("Halloween", "Christmas")) # Create the hidden object .hidden <- "LearnShareIT" # List all objects ls(all.names = TRUE)
Output
[1] ".hidden" "df" "vec"
List objects that satisfy the specified pattern
You can use the ls() function to list objects that satisfy the specified pattern by assigning the value to the ‘pattern’ parameters.
Look at the example below.
# Create a vector vec <- c("LearnShareIT", 3, 4, 5) # Create a data frame df <- data.frame(festivals = c("Halloween", "Christmas")) # Create the hidden object .hidden <- "LearnShareIT" # List objects whose names contain an v ls(all.names = TRUE, pattern = "v")
Output
[1] "vec"
Besides the ls() function, you can learn the series of R tutorials here.
Summary
You have learned the definition, and how to use the ls()
function in R. You can use the ls() function to list the specified objects in the specified environment. We hope this tutorial is helpful to you. Thanks!
My name is Thomas Valen. As a software developer, I am well-versed in programming languages. Don’t worry if you’re having trouble with the C, C++, Java, Python, JavaScript, or R programming languages. I’m here to assist you!
Name of the university: PTIT
Major: IT
Programming Languages: C, C++, Java, Python, JavaScript, R