regex – Create a variable in each dataframe that contains an extracted string from the dataframe name in R

I would like to create a variable in each dataframe in my environment that is a string from part of the dataframe name. For example, in the dummy data below, supposed I have two datasets named abc.dog1 and abc.dog2. I would like to create a variable in each dataset named ID, where ID="dog1" in the dataset abc_dog1 and ID="dog2" in the dataset abc_dog2. How would I create this variable?

EDIT: I will have up to 50 dataframes that I would like to do this for in my environment and I do not plan on merging them together.

EDIT 2: In my case, I would not know the exact words the name of the dataframes would contain, so I would like to grab the characters that come after “abc.” in the dataframe name. Thanks!

# load dummy data
data(cars)

# create two dummy datasets 
abc.dog1<-cars
abc.dog2<-cars

Read more here: Source link