使用关键字导入文件 [英] Import files using key words
问题描述
我想使用部分名称导入图像文件。我有100个.tif图像,其名称由3个不同的元素组成,例如Ai,Bi和Ci,例如:A1 B1 C1.tif,A1 B2 C1.tif,A1 B1 C2.tif, A2 B1 C1.tif...
我在代码的开头定义了Ai,Bi和Ci,并且想要调用包含这3个元素的文件。
I would like to import image files using parts of their names. I have 100 .tif images whose names are composed of 3 different elements, say Ai, Bi and Ci in such way: "A1 B1 C1.tif", "A1 B2 C1.tif", "A1 B1 C2.tif", "A2 B1 C1.tif"... I defined Ai, Bi and Ci at the beginning of the code, and want to call the file that contains these 3 elements.
我尝试过没有机会正确的选项,但找不到更好的选择:
I have tried options that have no chance to be correct, but cannot find anything better:
f = readTiff(Ai Bi Ci)
f = readTiff(Ai, Bi, Ci)
f = readTiff("Ai Bi Ci")
和使用readImage和file.name相同。 getwd给出了正确的路径。
提前谢谢。
and the same using readImage and file.name. The getwd gives the correct path. Thank you in advance.
推荐答案
您可以使用粘贴
将字符串粘合在一起的命令。
You can use the paste
command to glue strings together.
# for i for A, j for B selection and k for C selection
my.filename <- paste("A", i, " B", j, " C", k, ".tif", sep = "")
所以如果你想导入 A1 B2 C2.tif
i <- 1
j <- 2
k <- 2
my.filename <- paste("A", i, " B", j, " C", k, ".tif", sep = "")
注意paste0默认为sep =所以 paste0(A,i,B,j,C,k,。tt)
Note paste0 defaults to sep = "" so paste0("A", i, " B", j, " C", k, ".tif")
结果my.filename
results in my.filename
1) "A1 B2 C2.tif"
如果您正在使用路径:
my.filename <- paste("A", i, " B", j, " C", k, ".tif", sep = "")
my.path <- getwd() # or set this
readTiff(file.path(my.path, my.filename))
如果你想在循环中处理i,j,k的所有组合,那么你可以使用 file.exists
如果是,则导入它。
If you want to work through all combinations of i, j, k in loops then you can use file.exists
and if it does then import it.
注意:您使用哪个包 readTiff
?不要忘记确保在您的脚本中加载此包,使用 library(thispackage)
如果您收到找不到对象错误。
Note: Which package are you using for readTiff
? Do not forget to make sure this package is loaded in your script, with library(thispackage)
if you are getting "object not found" errors.
这篇关于使用关键字导入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!