Does this work for you?
I didn't get the part about generalizing to a large number of variables. Did you mean large number of excel files to read?
library(tidyverse); library(fs)
#create listing (fs_path object) of relevant files to read
excel_files <- dir_ls(regexp = "^test.*\\.xlsx$")
#read all files and store their name in `source` variable
all_files <- excel_files %>%
set_names() %>%
map_dfr(read_excel, .id = "source")
#remove extension of the file name and separate into two columns
all_files %>%
mutate(source = str_remove(source, "\\.xlsx$")) %>%
separate(source, into = c("test", "ID"), sep = "_")
Thanks @xvalda .
I have update the original post with a better example of the file name.
I also get an error about the combination of some columns .
Error in dplyr::bind_rows():
! Can't combine T1_123.xlsx$Column_A <<character.>> and T1_222.xlsx$Column_A <double.>.
Run rlang::last_error() to see where the error occurred.
There were 50 or more warnings (use warnings() to see the first 50)
In my original post I used the following so that all the columns will be to character. Is it possible to add mutate_all(as.character)) to your suggestion?