I have imported multiple excel files (more then 20) at once in R and I want to add values to columns in the dataframe based on the file name.
Let us say that the filename are the following: T1_123, T1_222 , T1_333 , T2_123, T2_222, T2_333 , T3_123, ect...
I want to create two column where the row in column_1 will show the T1, T2, T3.
So a row will show T1 if these row are from the file T1_123, T1_222 , T1_333
A row will show T2 if these row are from the file T2_123, T2_222, T2_333
column 2 should show 123 or 222 or 333 also based on the name of the file.
I have already imported all the files
#import all the excel files file.list <- list.files(path = "xxxxxxxxxx", pattern = "*.xlsx", full.names = TRUE) alldata<-file.list %>% map_dfr(~read_excel(.x)%>% mutate_all(as.character))
I need a solution that can be generalized to a large number of variables
How can I do this?
Thanks in advance