make a list of dataframes' names in R
To obtain a list of dataframe names in R, you can use the ls()
function combined with the class()
function. Here are the steps to do it:
Use the
ls()
function to retrieve the names of all objects in the current environment. Assign the result to a variable, let's sayobjects
.Use the
class()
function to determine the class of each object in theobjects
variable. This will help us filter out only the dataframe objects. Assign the result to a variable, let's sayobject_classes
.Use the
which()
function to identify the indices of theobject_classes
variable that have the value "data.frame". Assign the result to a variable, let's saydataframe_indices
.Use the
names()
function to retrieve the names of the objects in theobjects
variable that correspond to the indices in thedataframe_indices
variable. Assign the result to a variable, let's saydataframe_names
.Finally, print the
dataframe_names
variable to display the list of dataframe names.
Here is the code snippet that implements these steps:
objects <- ls()
object_classes <- sapply(objects, class)
dataframe_indices <- which(object_classes == "data.frame")
dataframe_names <- names(objects[dataframe_indices])
print(dataframe_names)
Please note that this code assumes that you want to retrieve the dataframe names in the current environment. If you want to retrieve dataframe names from a specific environment or workspace, you can modify the code accordingly by using the envir
parameter in the ls()
function.