2016-11-23 54 views
0

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

(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) 
+0

你有没有调查在你的代码失败具体点吗?使用'for'循环,你可以运行它们,让它们失败,然后检查输入值是什么(在你的情况下'j'和'i')。通过这些输入,运行代码的每一步以查看它在哪里以及如何失败。 – rosscova

+0

有两种方法可以处理“无字符串”,无论是为此测试还是使用if函数来避免或以某种方式处理它,或者使用tryCatch来捕获任何错误并处理它。通过这种方式,执行不会停止,并且该函数会继续运行。 –

回答

0

GS.dir<- list.dirs(path = ".", recursive = TRUE)列表下的"."的所有文件。当你再次致电list.files()时,你的脚本会做很多不必要的工作。例如list.files("./mydir/subdir/subsubdir/file.txt")将返回character(0)。我的看法是,您已经在代码的第一行中获得了所需的一切。然后,你可以通过你的regexpr子集的路径,这个载体:

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

之后,它可能更容易获得与dplyrdata.table代替嵌套循环完成任务。

[UPD]

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) 
+0

因此,我应该使用'files < - GS.dir文件< - list.files(GS.dir [j],pattern = glob2rx(“li * Avg.txt”),full.names = TRUE)' [grepl(“li * Avg.txt”),GS.dir)]' 这个不幸地不能解决错误 –

+0

我会尽量重现您的示例稍后。我知道它是否有效。 – utubun

+0

@JolienBracke我已经更新了答案。 – utubun