grepl returns a logical vector showing which strings match a pattern.

Program

Play the script to keep only names ending in .csv.

grepl_patterns.R
files <- c("report.csv", "notes.txt", "sales.csv")
csv_files <- files[grepl("\\.csv$", files)]
count <- length(csv_files)
cat(count, "\n", sep = "")
grepl `grepl(pattern, files)` returns `TRUE` or `FALSE` for each file name.
regular expression `\\.csv$` means a literal `.csv` at the end of the string.
length `length(csv_files)` counts selected vector elements.