user6016731 user6016731 - 3 months ago 10
R Question

Dynamic column creation based on suffix of dataframe

I have multiple .csv files in a folder with different suffix . For eg :

Data_Software
Data_Hardware
Data_Manufacturing ....


& similarly many other .csv files. I want to create a new column in each of these data sets say "type" which will contain the suffix of the corresponding file i.e.; all the observations of the type column in the Data_Software should say Software , Data_Hardware should have Hardware.

Can someone help ?

Answer

While I wouldn't advise, I might do something like:

library(data.table) # need for fread and :=

# Get a list of all files in the directory 
my_dir <- "my_path_here"    
FILES <- list.files(path = my_dir, pattern="*.csv$", full.names = TRUE, recursive = FALSE)

# Read every file
lapply(FILES, function(x) { assign(gsub(paste0(my_dir,"/|\\.csv$|Data_"),"",x),fread(x, header = T)[, Type := gsub(paste0(my_dir,"/|\\.csv$|Data_"),"",x)], envir = .GlobalEnv)})

This creates a table for each csv - the table is named the same name as the file, stripping the extension, path, and Data_. It also creates a column with the table name on read