Reputation: 121
I have a list of files, which was created using list.files:
filesxx<-list.files(pattern="0-4000")
obtaining a list like the following one:
[1] "Alta Guajira Coord & elevation TS 0-4000.txt" "Baja Guajira Coord & elevation TS 0-4000.txt"
[3] "Bajo Meta Coord & elevation TS 0-4000.txt" "Rio Arauca Coord & elevation TS 0-4000.txt"
[5] "Rio Catatubo Coord & elevation TS 0-4000.txt" "Rio Cesar Coord & elevation TS 0-4000.txt"
[7] "Rio Sogamoso Coord & elevation TS 0-4000.txt" "Rio Tomo Coord & elevation TS 0-4000.txt"
[9] "Sabana de Bogota Coord & elevation TS 0-4000.txt" "Total Area Coord & elevation TS 0-4000.txt"
and from this list I want create a loop that will select one of this files depending on the loop variable (a) which will be a pattern in the name, for example the variable (a) will be: "Alta Guajira" and in the next cycle "Rio Tomo".. etc.
How can I do this, how to chose a file with a pattern from a list of files already made?
I think the function Shopt
is more to exclude patterns and maybe the function find
could work but after looking for some similar case I haven't found the answer.
Upvotes: 1
Views: 3439
Reputation: 7164
You can list the files in your working directory with list.files()
. It has an argument allowing you to specify a certain pattern:
list.files(pattern = "Alta Guajira")
# [1] "Alta Guajira Coord & elevation TS 0-4000.txt"
If you have a vector with the patterns in there, like:
patterns <- c("Alta Guajira", "Rio Tomo", "Rio Sogamoso")
sapply(patterns, function(x){list.files(pattern = x)})
If you already have the file names, you are basically looking for patterns in a character vector.
df <- c("Alta Guajira Coord & elevation TS 0-4000.txt","Baja Guajira Coord & elevation TS 0-4000.txt",
"Bajo Meta Coord & elevation TS 0-4000.txt", "Rio Arauca Coord & elevation TS 0-4000.txt",
"Rio Catatubo Coord & elevation TS 0-4000.txt", "Rio Cesar Coord & elevation TS 0-4000.txt",
"Rio Sogamoso Coord & elevation TS 0-4000.txt", "Rio Tomo Coord & elevation TS 0-4000.txt",
"Sabana de Bogota Coord & elevation TS 0-4000.txt", "Total Area Coord & elevation TS 0-4000.txt")
patterns <- c("Alta Guajira", "Rio Tomo", "Rio Sogamoso")
it <- 0
res <- c()
for(i in patterns){
it <- it + 1
res <- append(res, df[grepl(pattern = i, x = df)])
}
res
# [1] "Alta Guajira Coord & elevation TS 0-4000.txt" "Rio Tomo Coord & elevation TS 0-4000.txt"
# [3] "Rio Sogamoso Coord & elevation TS 0-4000.txt"
Or, alternatively:
res2 <- sapply(patterns, function(y){df[grepl(pattern = y, x = df)]})
Upvotes: 3