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:

  1. Use the ls() function to retrieve the names of all objects in the current environment. Assign the result to a variable, let's say objects.

  2. Use the class() function to determine the class of each object in the objects variable. This will help us filter out only the dataframe objects. Assign the result to a variable, let's say object_classes.

  3. Use the which() function to identify the indices of the object_classes variable that have the value "data.frame". Assign the result to a variable, let's say dataframe_indices.

  4. Use the names() function to retrieve the names of the objects in the objects variable that correspond to the indices in the dataframe_indices variable. Assign the result to a variable, let's say dataframe_names.

  5. 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.