0

I have a folder that contain many file formats, how can I only select txt file extensions?

lf <- list.files("E:/UUU/", full.names=TRUE)
elyraz
  • 473
  • 5
  • 18

1 Answers1

2

We need to use the pattern argument to match all files with a . (as . is a metacharacter, we escape (\\) it) followed by the string 'txt' and specify that it is at the end ($) of the string

lf <- list.files(path = "E:/UUU/", pattern = "\\.txt$", full.names=TRUE)

By default, the pattern is set as NULL, so it will select all the files in the folder. If we check the Usage from ?list.files

list.files(path = ".", pattern = NULL, all.files = FALSE, full.names = FALSE, recursive = FALSE, ignore.case = FALSE, include.dirs = FALSE, no.. = FALSE)

akrun
  • 874,273
  • 37
  • 540
  • 662