1

I have several files in one dirctory: I list them all using:

  dir1<- list.files ("/data/", "*.txt", full.names = TRUE)
  Gh_12_kj.txt   
  kh_12_k.txt 
  Gh_13_kj.txt 
  kh_13_k.txt 

I can read them one by one like this:

  for (i in seq_along(dir1)) {file =read.table(i) ……}

I want to combine all files with similar names but different numbers for example:

  Rbind Gh_12_kj.txt and Gh_13_kj.txt 
  Rbind s_13_f.txt and s_12_f.txt     and so on for all files in this dirctory

I guess we need unique but not sur how

bic ton
  • 1,284
  • 1
  • 9
  • 16

1 Answers1

1

As the 'dir1' object was created with the full.names = TRUE option from list.files, we can extract the file name with basename and file_path_sans_ext (from tools), then split the 'dir1' by the substring of 'files' i.e. only keeping the 'Ghkj_df', 'khf_df' etc. in 'Gh_12_kj_df.txt', 'kh_13_f_df.txt'), loop through the nested list elements with lapply, then loop again and read the files with fread and rbind them together with rbindlist.

library(data.table)
library(tools)
files <- file_path_sans_ext(basename(dir1))  
lapply(split(dir1, sub("^([^_]+)_\\d+_([^.]+)", "\\1\\2", files)), 
                          function(x) rbindlist(lapply(x, fread)))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • @bicton It is not clear about the error. What do you get with `split(dir1, sub("_.*", "", files))` – akrun Aug 29 '16 at 16:53
  • @bicton Based on your description in the post, these files are grouped togher and `rbind`ed and that is what the code does. i.e. `v1 <- c("Gh_12_kj.txt", "kh_12_k.txt", "Gh_13_kj.txt", "kh_13_k.txt"); split(v1, sub("_.*", "", v1))` – akrun Aug 29 '16 at 16:58
  • @bicton Yes, by using the `sub`, we are extracting only the `kh` or `Gh` part and so we get the `Gh` files all as a list element, likewise `kh` as another, then we loop through each of the nested elements, and read it with `fread`, `rbind` it together – akrun Aug 29 '16 at 17:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122127/discussion-between-akrun-and-bic-ton). – akrun Aug 29 '16 at 17:01