Reputation: 519
I would like to read only selected .txt files in a folder to construct a giant table... I have over 9K files, and would like to import the files with the selected distance and building type, which is indicated in part of the file name.
For example, I want to first select files with name containing "_U0" and "_0_Final.txt":
Type = c(0,1)
D3Test = 1
Distance = c(0,50,150,300,650,800)
D2Test = 1;
files <- list.files(path=data.folder, pattern=paste("*U", Type[D3Test],"*_",Distance[D2Test],"_Final.txt",sep=""))
But the result returned empty... Any problem with my construction?
filename <- scan(what="")
"M10_F1_T1_D1_U0_H1_0_Final.txt" "M10_F1_T1_D1_U0_H1_150_Final.txt" "M10_F1_T1_D1_U0_H1_300_Final.txt"
"M10_F1_T1_D1_U0_H1_50_Final.txt" "M10_F1_T1_D1_U0_H1_650_Final.txt" "M10_F1_T1_D1_U0_H1_800_Final.txt"
"M10_F1_T1_D1_U0_H2_0_Final.txt" "M10_F1_T1_D1_U0_H2_150_Final.txt" "M10_F1_T1_D1_U0_H2_300_Final.txt"
"M10_F1_T1_D1_U0_H2_50_Final.txt" "M10_F1_T1_D1_U0_H2_650_Final.txt" "M10_F1_T1_D1_U0_H2_800_Final.txt"
"M10_F1_T1_D1_U0_H3_0_Final.txt" "M10_F1_T1_D1_U0_H3_150_Final.txt" "M10_F1_T1_D1_U0_H3_300_Final.txt"
"M10_F1_T1_D1_U0_H3_50_Final.txt" "M10_F1_T1_D1_U0_H3_650_Final.txt" "M10_F1_T1_D1_U0_H3_800_Final.txt"
"M10_F1_T1_D1_U1_H1_0_Final.txt" "M10_F1_T1_D1_U1_H1_150_Final.txt" "M10_F1_T1_D1_U1_H1_300_Final.txt"
"M10_F1_T1_D1_U1_H1_50_Final.txt" "M10_F1_T1_D1_U1_H1_650_Final.txt" "M10_F1_T1_D1_U1_H1_800_Final.txt"
Upvotes: 3
Views: 1118
Reputation: 263362
You should look at the result that you are passing to pattern
:
"*U0*_0_Final.txt"
It is not going to pick up any of those filenames. The asterisk is saying zero or more instances of "0" between "U" and the underscore. If Type and Distance are not represented by T and D in the file names, then this delivers the correct pattern:
grep( pattern=paste0("_U", Type[D3Test],".*_", Distance[D2Test],"_Final\\.txt"), filename)
#-----------
#[1] 1 7 13 So matches 3 filenames
Notice that you need to escape (with two backslashes) the periods that you want to be only periods because periods are special characters. You also need to use ".*" to allow a gap in the pattern.
Upvotes: 2
Reputation: 519
files <- list.files(path=data.folder, pattern=paste("*U", Type[D3Test], "....",Distance[D2Test], sep=""))
I revised my code and this one works! Basically the idea is to use dot to present each character between Type[D3Test] and Distance[D2Test], since the characters between these two are fixed at 4.
Thanks to: http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/
Upvotes: 0
Reputation: 70633
Another way would be to use sprintf
and grepl
.
x <- c("M10_F1_T1_D1_U0_H1_150_Final.txt", "M10_F1_T1_D1_U0_H2_650_Final.txt", "M10_F1_T1_D1_U1_H1_650_Final.txt")
x[grepl(sprintf("U%i_H%i_%i", 1, 1, 650), x)]
[1] "M10_F1_T1_D1_U1_H1_650_Final.txt"
Upvotes: 2