bic ton bic ton - 1 year ago 56
R Question

how to combine two files in R?

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

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

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
but not sur how

Answer Source

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.

files <- file_path_sans_ext(basename(dir1))  
lapply(split(dir1, sub("^([^_]+)_\\d+_([^.]+)", "\\1\\2", files)), 
                          function(x) rbindlist(lapply(x, fread)))