Load files with certain names in R

时间:2018-03-25 20:59:57

标签: r file load

I have thousands of files, but I only want to load some of them, those I want to load all are all named "xxxx5429.spe" e.i the all have the same ending, is there a easy way to do it?

1 个答案:

答案 0 :(得分:3)

Guess those x's are meant to be wildcards. Also guessing they are meant to be exactly 4 characters wide. If my guesses are both correct then probably can be handled witha regex pattern like:

 patt= ".{4}5429[.]spe"

The list.files function can handle a regex pattern to its second argument. The first argument needs to be a path. Or you can do this just in the working directory if you name the argument:

 my_files <- list.files(patt= ".{4}5429[.]spe")

It's not exactly clear what you mean by "load files". The term load in R generally applies to a package or a .Rdata file. If these are text files then one of the read* functions would be used: the readLines function if the text is unstructured, or one of the variants of the read.table function if the files are rectangular and delimited files.

This would create a list object containing character vectors with the contents of such files (from the working directory) using readLines:

  my_files <- lapply( my_files, readLines)
相关问题