Reputation: 135
I've got a list of files that I want to import into R. They look like this:
> dput(asia)
"chin074.csv", "indi001.csv", "indi001e.csv", "indi001l.csv",
"indi001n.csv", "indi001x.csv", "indi002.csv", "indi002e.csv", ...
I want to import only those files that do not have a letter after the main name. So, I want to import chin074.csv
and indi001.csv
but not indi001e.csv
or indi001l.csv
, which have letters e
and l
after the main title.
I know I can use some variation of grep
to do this, but I can't figure out how. So something like this:
asia<-grep(list.files(path="./asia/"), pattern='?', inv=T, value=T)
asia_new<- lapply(asia, read.csv)
How do I do this?
Upvotes: 2
Views: 42
Reputation: 887901
We can also do
grep("[a-z]+[0-9]+\\.csv", asia, value = TRUE)
#[1] "chin074.csv" "indi001.csv" "indi002.csv"
asia <- c("chin074.csv", "indi001.csv", "indi001e.csv", "indi001l.csv",
"indi001n.csv", "indi001x.csv", "indi002.csv", "indi002e.csv")
Upvotes: 1
Reputation: 389275
You can use the pattern :
asia <- grep('[a-z]+\\d+\\.csv', asia, value = TRUE)
#[1] "chin074.csv" "indi001.csv" "indi002.csv"
We select the files which has one or more lower case letters (a-z
), followed by one or more numbers, followed by .csv
.
data
asia <- c("chin074.csv", "indi001.csv", "indi001e.csv", "indi001l.csv",
"indi001n.csv", "indi001x.csv", "indi002.csv", "indi002e.csv")
Upvotes: 2