'How I can select a file with R, in this script

Hi this is my script and I want only files with name 'pancreas'. How can I add this in my script? I have txt files.

This is my script:

  1. generate list of files in folder:
files <- data.frame(list.files(folder, recursive = TRUE, full.names = TRUE, pattern = '\\.txt$')[1:200])
colnames(files)[1] <- "path"
  1. drop records containing "slide.txt" in file name:
files <- subset(files, !grepl("slide.txt", files$path, fixed = TRUE))

Here's the screen shot of my codes:

enter image description here



Solution 1:[1]

files <- list.files(".", pattern = "\\.txt$", recursive = TRUE)
files
#> [1] "foo.txt"       "pancreas1.txt" "pancreas2.txt" "slide.txt"
pancreas_files <- grep(pattern = "pancreas", x = files, value = TRUE)
pancreas_files
#> [1] "pancreas1.txt" "pancreas2.txt"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 danlooo