5

下面给出的代码运行良好,并读取了我目录中的文件并提取了值:

X <- c(75:85) ; Y <- c(208:215) 
extract <- vector()
files <- list.files("C:\\New folder (10)", "*.img",full.names=TRUE)

}

我试图通过使用 sprintf 来指定它,但我得到了错误。请提供任何帮助:

for (i in c(1:365)) {
   fileName <- sprintf("C:New folder (10)/Climate_Rad_%d.img", i)
}
4

4 回答 4

6

啊,好的,我明白了,问题出在排序上。排序顺序按字母顺序正确。Climate_Rad_1 按字母顺序后跟 Climate_Rad_10,而不是 Climate_Rad_2 顺序根本不是“随机”的,它按字母顺序是正确的。

但是,您希望在 Climate_Rad_10 之前处理 Climate_Rad_2,而不是之后。有几种方法可以解决这个问题。首先,您应该注意,Climate_Rad_002 确实按字母顺序排在 Climate_Rad_010 之前,因此如果您在生成文件时添加前导零,这将便于以后按数字顺序处理文件。

或者,假设您在创建文件时无法添加零。然后至少有两种方法可以按顺序访问文件。或者通过在文件名之后添加零,或者只是对文件名的数字部分进行排序。

让我告诉你后者。

myFiles <- paste("Climate_Rad_", c(1:15, 95:110), ".img", sep = "") # create some test names, you get the actual myFiles through a call to list.files()

myFiles.sorted <- sort(myFiles) # this gives the alphabetic sorting, not what you want

> myFiles.sorted
 [1] "Climate_Rad_1.img"   "Climate_Rad_10.img"  "Climate_Rad_100.img"
 [4] "Climate_Rad_101.img" "Climate_Rad_102.img" "Climate_Rad_103.img"
 [7] "Climate_Rad_104.img" "Climate_Rad_105.img" "Climate_Rad_106.img"
[10] "Climate_Rad_107.img" "Climate_Rad_108.img" "Climate_Rad_109.img"
[13] "Climate_Rad_11.img"  "Climate_Rad_110.img" "Climate_Rad_12.img" 
[16] "Climate_Rad_13.img"  "Climate_Rad_14.img"  "Climate_Rad_15.img" 
[19] "Climate_Rad_2.img"   "Climate_Rad_3.img"   "Climate_Rad_4.img"  
[22] "Climate_Rad_5.img"   "Climate_Rad_6.img"   "Climate_Rad_7.img"  
[25] "Climate_Rad_8.img"   "Climate_Rad_9.img"   "Climate_Rad_95.img" 
[28] "Climate_Rad_96.img"  "Climate_Rad_97.img"  "Climate_Rad_98.img" 
[31] "Climate_Rad_99.img" 

# split between the part that comes before the numerics and the "1.img" etc.--adjust appropriately
split <- strsplit(myFiles.sorted, "Climate_Rad_") 
# strip the "1.img" etc such that only the numeric part is left
# turn the characters in numeric
split <- as.numeric(sapply(split, function(x) x <- sub(".img", "", x[2])))
# not you can sort, by using order, that gives the original filenames, ordered on the numeric part of the filename
myFiles.correct.order <- myFiles.sorted[order(split)]

 [1] "Climate_Rad_1.img"   "Climate_Rad_2.img"   "Climate_Rad_3.img"  
 [4] "Climate_Rad_4.img"   "Climate_Rad_5.img"   "Climate_Rad_6.img"  
 [7] "Climate_Rad_7.img"   "Climate_Rad_8.img"   "Climate_Rad_9.img"  
[10] "Climate_Rad_10.img"  "Climate_Rad_11.img"  "Climate_Rad_12.img" 
[13] "Climate_Rad_13.img"  "Climate_Rad_14.img"  "Climate_Rad_15.img"   
[16] "Climate_Rad_95.img"  "Climate_Rad_96.img"  "Climate_Rad_97.img" 
[19] "Climate_Rad_98.img"  "Climate_Rad_99.img"  "Climate_Rad_100.img"
[22] "Climate_Rad_101.img" "Climate_Rad_102.img" "Climate_Rad_103.img"
[25] "Climate_Rad_104.img" "Climate_Rad_105.img" "Climate_Rad_106.img"
[28] "Climate_Rad_107.img" "Climate_Rad_108.img" "Climate_Rad_109.img"
[31] "Climate_Rad_110.img"

这将按照您要查找的顺序为您提供文件。现在根据那个拉文件,例如通过

for (fileNames in myFiles.correct.order) {READ.IN.AND.DO.YOUR.THING}

那应该这样做。确保根据文件名调整“Climate_Rad_”和“.img”(您可能还必须在“Climate_Rad_”之前添加一个路径,使其类似于“C:/filefolder/Climate_Rad_”,如果是这样的话需要)。

于 2012-05-28T18:56:45.043 回答
5

为什么不使用 list.files() 检索所有文件(具有特定模式)然后对其进行排序。然后你从排序的向量中检索文件,它以正确的排序顺序将它们提供给你。这样做的好处是,当 1:365 序列中缺少数字时它也可以工作

就像是:

myFiles <- list.files(pattern = "^Climate_Rad_") #all files starting with Climate_
myFiles <- sort(myFiles)
# then read them in, for instance through
for (fileNames in myFiles) {READ.IN.AND.DO.YOUR.MAGIC.ON.THEM}
于 2012-05-27T23:36:00.937 回答
2

gtools包具有可以帮助您的功能“混合排序”。

myFiles示例取自Peter Verbeet 的回答。

myFiles <- paste("Climate_Rad_", c(1:15, 95:110), ".img", sep = "") 

install.packages ('gtools')
require ('gtools')

mixedsort (myFiles)

[1] "Climate_Rad_1.img"   "Climate_Rad_2.img"  
[3] "Climate_Rad_3.img"   "Climate_Rad_4.img"  
[5] "Climate_Rad_5.img"   "Climate_Rad_6.img"  
[7] "Climate_Rad_7.img"   "Climate_Rad_8.img"  
[9] "Climate_Rad_9.img"   "Climate_Rad_10.img" 
[11] "Climate_Rad_11.img"  "Climate_Rad_12.img" 
[13] "Climate_Rad_13.img"  "Climate_Rad_14.img" 
[15] "Climate_Rad_15.img"  "Climate_Rad_95.img" 
[17] "Climate_Rad_96.img"  "Climate_Rad_97.img" 
[19] "Climate_Rad_98.img"  "Climate_Rad_99.img" 
[21] "Climate_Rad_100.img" "Climate_Rad_101.img"
[23] "Climate_Rad_102.img" "Climate_Rad_103.img"
[25] "Climate_Rad_104.img" "Climate_Rad_105.img"
[27] "Climate_Rad_106.img" "Climate_Rad_107.img"
[29] "Climate_Rad_108.img" "Climate_Rad_109.img"
[31] "Climate_Rad_110.img"
于 2017-01-22T14:44:15.520 回答
1

正如我在上面的评论中提到的,您可以使用它paste来简化您的代码。尝试:

# set the working directory
setwd("C:New folder (10)")

# construct a vector of file names and loop through
for (f in paste("Climate_Rad_", 1:365, ".img", sep="")) {
    conne <- file(f, "rb")
    # do rest, assuming it's correct
}
于 2012-05-27T21:34:34.487 回答