user6016731 user6016731 - 1 year ago 69
R Question

Dynamic column creation based on suffix of dataframe

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

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 Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download