I created a list of dataframes. I need to loop over them, filter what I need and save as a single file. However, I need to know from each file each values comes from.
Each dataframe has a name like Plastic Chair 1111, Wooden Chair 3950, Table 6909, etc... and are saved inside a list named "listed" that contains the following structure:
listed[1] Material_ID ABC Key.Figure W01 W02 W03 46548970 A Actuals 1048 564 548 46548970 A Forecasted 848 500 590 18969856 A Actuals 358 1500 900 18969856 A Forecasted 460 1602 1000 listed[2] Material_ID ABC Key.Figure W01 W02 W03 24564897 A Actuals 1258 444 798 26548970 A Forecasted 1345 500 850 34879856 A Actuals 985 1020 980 15486856 A Forecasted 846 1064 1100 What I would like to obtain is:
Group name Group Code Material_ID ABC Key.Figure W01 W02 W03 Plastic Chair 1111 46548970 A Actuals 1048 564 548 Plastic Chair 1111 18969856 A Actuals 358 1500 900 Wooden Chair 3950 24564897 A Actuals 1258 444 798 Wooden Chair 3950 34879856 A Actuals 985 1020 980 Is it possible to create these two columns on the left by using the dataframes name?
Thank you very much for the help!
Here is my code if you need to better understand the situation.
library(openxlsx) library(dplyr) library(purrr) # read the data filename = 'Dataset.xlsx' wb <- loadWorkbook(filename) # get a list of the spreadshits in the excel file sheetNames <- sheets(wb) sheetNames <- make_names(sheetNames) # create an empty list listed <- list() # assign which spreadshit as a dataframe inside a list for(i in 1:length(sheetNames)) { listed[[i]] <- assign(sheetNames[i],readWorkbook(wb,sheet = i)) print(paste0("read the ", i," file")) # here it says what it's doing } # remove variable Sales.Org.ID map(listed, ~ (.x %>% select(-Sales.Org.ID))) # filter the dataframes to only show rows with Key.Figure = "Actual Totals" list_actuals <- lapply(listed, function(x) x %>% filter( Key.Figure == "Actual Totals"), ) # put the result in a single dataframe result_actuals = do.call(rbind,list_actuals)
purrr::map_dfrwhich works for a list of named dataframes appending the dataframe name using the.idargument?datacontaining each dataframe and then mutate a function to do the filtering, and then do anrbindon the mutated column.