【问题标题】:Can list.files() ignore paths which do not contain the pattern in R?list.files() 可以忽略不包含 R 中模式的路径吗?
【发布时间】:2016-11-23 08:41:59
【问题描述】:

我想合并来自不同文件夹的数据。因此,我首先在当前工作目录中创建一个包含所有地图名称的对象(使用 list.dirs())。之后,我在每张地图中查找特定文件名(我的模式)。 问题是当文件不包含此特定字符串时, list.files() 会出错。

(Error in file(file, "rt") : cannot open the connection In addition: Warning message: In file(file, "rt") : cannot open file 'NA': No such file or directory).

当我只选择我知道包含该模式的地图时,代码有效。

有人知道如何让 list.files() 忽略不包含特定模式的路径吗?

这是我的代码:

GS.dir<- list.dirs(path = ".",  recursive = TRUE)
ligustrum <- c()

    for (j in 1:length(GS.dir)){          
        files <- list.files(GS.dir[j], pattern = glob2rx("li*Avg.txt"), full.names = TRUE)
        if(!is.null(files)){
            for (i in 1:length(files)){
                plot <- read.table(files[i], header = TRUE, sep = ",")
                datum <- substr(files[i], 1, 8)
                nummer <- substr(files[i], nchar(files[i]) - 7, nchar(files[i]) - 7)
                plot.date <- data.frame("Date" = rep(datum, length(plot[,1])), 
                                        "plotnr"=rep(nummer,length(plot[,1])), 
                                         plot
                                        )
                ligustrum <- rbind(ligustrum, plot.date)
            }
        } else {
            ligustrum <- ligustrum
        }
     }

    write.table(ligustrum, "ligustrum.txt", sep = ";", row.names = FALSE)

【问题讨论】:

  • 您是否调查过代码失败的具体点?使用for 循环,您可以运行它们,让它们失败,然后检查输入值是什么(在您的情况下为ji)。使用这些输入,运行代码的每个步骤以查看失败的位置和方式。
  • 有两种方法可以处理“无字符串”,或者对此进行测试并使用if 函数以某种方式避免或处理它,或者使用tryCatch 捕获任何错误并处理它.这样执行就不会停止,函数会运行得更远。

标签: r


【解决方案1】:

GS.dir&lt;- list.dirs(path = ".", recursive = TRUE) 列出了"." 下的所有文件。当你再次调用list.files() 时,你的脚本会做很多不必要的工作。例如list.files("./mydir/subdir/subsubdir/file.txt") 将返回character(0)。我的观点是,你已经在第一行代码中得到了你需要的一切。然后你可以通过你的正则表达式子集这个路径向量:

GS.files <- list.dirs(path = ".",  recursive = TRUE)
files <- GS.files[grepl("li*Avg.txt", GS.files)]

之后,使用dplyrdata.table 而不是嵌套循环可能更容易完成工作。

[更新]

require(dplyr)

data.frame(files = list.files(recursive = T)) %>%
        filter(grepl("li(.)*Avg.txt",files)) %>%
        mutate(files = as.character(files)) %>%
        group_by(files) %>%
        mutate(datum = substr(substr(files, 
                                     max(gregexpr("/",files)[[1]])+1, 
                                     nchar(files)
                                     ), 1, 8),
               nummer = substr(files, nchar(files) - 7, nchar(files) - 7)) %>%
        do(data.frame(datum = as.Date(.$datum, format = "%Y%m%d"),
                      nummer = as.numeric(.$nummer), 
                      read.table(.$files, T))) %>%
        arrange(datum, nummer, files) %>%
        write.table(.,file = "ligstrum.txt", sep = ";", row.names = F)

【讨论】:

  • 所以不是 'files Avg.txt"), full.names = TRUE)', 我应该使用 'files Avg.txt"), GS.dir)]' 不幸的是这并不能解决错误
  • 稍后我会尝试重现您的示例。我知道它是否有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-28
  • 1970-01-01
  • 1970-01-01
  • 2021-11-22
  • 2019-10-19
  • 1970-01-01
相关资源
最近更新 更多