1

I need some help for reading multiple files (1.snplist,2.snplist, 3.snplist...) at once. Right now, I am using this,

library(data.table)
a1 <- fread('1.snplist')
a2 <- fread('2.snplist')
a3 <- fread('3.snplist')

How can I read all files in R at once, with different file names, a1,a2,a3...a22. Thank you

Muhammad
  • 145
  • 3

1 Answers1

2

First, you need to list all files that you want to read. Then, you could use a loop to capture the data in a list like so:

filelist <- list.files(pattern='.snplist')
datalist <- list()
for(i in seq_along(filelist)) {
  datalist[[i]] <- fread(filelist[i])
}

Note we use seq_along instead of 1:length(filelist) to avoid errors in case filelist is empty (length 0).

gaut
  • 5,771
  • 1
  • 14
  • 45