0

I am importing multiple csv files from a list. I would like to add a column to each imported dataset with a unique name.

datasets <- list2env(lapply(setNames(temp_dirs, str_before_first(str_after_nth(temp_dirs, "/", -2),"/")), read.csv), .GlobalEnv)

temp_dirs = a list of file paths

Each dataset is saved using a section of the file path name. For each dataset, I would like to add a column so that the file path (either a section or the entire path) is inserted into a column within each dataset. How can I do this?

Thanks

L-dan
  • 19
  • 2
  • 1
    The `data.table` package has this functionality built in to its `rbindlist()` function – DanY Jun 27 '23 at 15:24
  • 2
    I would strongly encourage you not to use `list2env`. It's much easier to work with data in R if you keep it in a list rather than make a bunch of different global varibales. – MrFlick Jun 27 '23 at 15:24
  • If I use `rbindlist()` how can I create a column that identifies each of the datasets (i.e. an identifying column? – L-dan Jun 27 '23 at 15:26
  • Yes, see `?rbindlist` – Axeman Jun 27 '23 at 15:27
  • glob the files into a list and then use `read_csv()` from `readr` and use the `id='path'` argument like so `df <- read_csv(files, id='path')` – stefan_aus_hannover Jun 27 '23 at 15:33

1 Answers1

-1

Use the idcol argument from data.table's rbindlist() function:

vec_of_filenames <- c("file1.csv", "file2.csv")

list_of_data_from_csv_files <- vector(mode="list", length=length(vec_of_filenames))

for(i in seq_along(vec_of_filenames)) {
    list_of_data_from_csv_files[i] <- read.csv(vec_of_filenames[i])
}

final_data <- data.table::rbindlist(list_of_data_from_csv_files, idcol=TRUE)
DanY
  • 5,920
  • 1
  • 13
  • 33