Enric Agud Pique
Enric Agud Pique

Reputation: 1105

selecting files.csv in R

I need to select all the files inside a folder in format .csv that contains only non numerical characters.

I use the following code, but it selects only 9 files of 13 with the chosen pattern. Is it right?

I select files like Berlin.csv

filenames <- list.files(pattern="[:alpha:].csv", full.names=TRUE)
ldf <- lapply(filenames, read.csv, header = FALSE)
length(ldf)
ldf

Upvotes: 1

Views: 51

Answers (1)

Thomas
Thomas

Reputation: 44525

You want something like:

list.files(pattern = "^[[:alpha:]]+\\.csv")

That pattern will match any CSV that starts with and contains only alphabetical characters. But, if you want to allow filenames with other non-alphabetic characters (e.g., spaces, punctuation), use something like this:

list.files(pattern = "^[^[:digit:]]+\\.csv")

That will just exclude any filenames that have a number in them. (Note the two different meanings of ^ when used inside and outside of a character class.)

Upvotes: 4

Related Questions