Extract name of data.frame in R as character
a <- data.frame()
deparse(substitute(a))
[1] "a"
This is also how plot
knows what to put on the axes
If you've got more than one dataframe that you want to retrieve the name of, you can use ls.str(mode = "list")
. We use list because dataframes are stored as lists.
For example:
# make two dataframes
mydf1 <- data.frame(a = letters[1:10], b = runif(10))
mydf2 <- data.frame(a = letters[1:10], b = runif(10))
# see that dataframes are stored as lists:
storage.mode(mydf1)
[1] "list"
# store the names of the dataframes
names_of_dataframes <- ls.str(mode = "list")
# see the name of the first dataframe
names_of_dataframes[1]
[1] "mydf1"
# see the name of the second dataframe
names_of_dataframes[2]
[1] "mydf2"
Note that this method will also include the names of other list objects in your global environment. ls.str
allows you to select the environment you want to search so you could store your dataframes in a different environment to other list objects.