1

I'm reading and combining a large group of csv tables into R but before merging them all I'll like to create a column with the name of the file where those specific set of rows belong.

Here is an example of the code I wrote to read the list of files:

archivos <- list.files("proyecciones", full.names = T)
#proyecciones is the folder where all the csv files are located.
tbl <- lapply(archivos, read.table, sep="", head = T) %>% bind_rows()

As you can see I already have the names of the files in "archivos" but still haven't been able to figure it out how to put it into the lapply command.

Thanks!

Guillermo.D
  • 399
  • 2
  • 14
  • just nice someone asked this recently: https://stackoverflow.com/questions/49100250/add-file-name-to-appended-dataset-for-each-file-in-r the comments there should be useful – chinsoon12 Mar 05 '18 at 01:42
  • Could you please tell me whether the code in the solution works? – akrun Mar 05 '18 at 02:41

1 Answers1

1

We need to use the .id in bind_rows

lapply(archivos, read.table, sep="", header = TRUE) %>%
    set_names(archivos) %>%
    bind_rows(.id = 'grp')

A more tidyverse syntax would be

library(tidyverse)
map(archivos, read.table, sep='', header = TRUE) %>%
     setnames(archivos) %>%
     bind_rows(.id = 'grp')
akrun
  • 874,273
  • 37
  • 540
  • 662
  • It worked well, but not completely. Now that I have the "grp" column with the index number of the "archivos" file. How can change that number into the name of that specific file? – Guillermo.D Mar 05 '18 at 02:43
  • @Guillermo.D I got it why you are getting that. Because the list names are empty. Just use `set_names` (updated the code) – akrun Mar 05 '18 at 02:49