Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用for循环将R中不同子文件夹中的多个文本文件绑定在一起_R_Loops_For Loop_Subdirectory - Fatal编程技术网

使用for循环将R中不同子文件夹中的多个文本文件绑定在一起

使用for循环将R中不同子文件夹中的多个文本文件绑定在一起,r,loops,for-loop,subdirectory,R,Loops,For Loop,Subdirectory,我以前见过有人问过这个问题,但它们对解决我的问题没有多大帮助 我想从150个子文件夹中绑定(rbind)多个文本文件,但我只对每个文件夹中的2个不同文件感兴趣。详情如下: 名为“文件夹”的主文件夹,其中包含150个子文件夹 在每个子文件夹中,我想取出以1和11开头的文件。(即1_HDx1.txt和11_HDx1.txt)-每个文件夹中有2个文件 在数据框中创建一列,该列具有从中提取文件的子文件夹的名称 如果所有文件都在一个文件夹中,我知道该怎么做(见下文),但我能为文件夹文件夹做类似的事情吗

我以前见过有人问过这个问题,但它们对解决我的问题没有多大帮助

我想从150个子文件夹中绑定(
rbind
)多个文本文件,但我只对每个文件夹中的2个不同文件感兴趣。详情如下:

  • 名为“文件夹”的主文件夹,其中包含150个子文件夹
  • 在每个子文件夹中,我想取出以1和11开头的文件。(即1_HDx1.txt和11_HDx1.txt)-每个文件夹中有2个文件
  • 在数据框中创建一列,该列具有从中提取文件的子文件夹的名称
如果所有文件都在一个文件夹中,我知道该怎么做(见下文),但我能为文件夹文件夹做类似的事情吗

z <- NULL
files <- dir("Folder")
for (file in files) {
  x <- read.csv(file.path("Folder", file), as.is=TRUE)
  x$source <- substring(file, 8, 10) #name of file for the source
 z <- rbind(z, x)]) 
}

我建议使用一系列的
apply
函数来实现这一点

假设以文件夹完整目录作为工作目录开始:

Folders <- list.files() # creates a list of all the folders

# Looks in each folder and returns a path to all files starting with 1 or 11.
Paths <- lapply(Folders,function(x){
  F <- list.files(x)
  F <- F[grepl("^(1|11)",F)]
  paste0(x,'/',F)
})
Paths <- unlist(Paths)

# Reads each of the selected files into a list.
Tables <- lapply(Paths, function(Path){
  read.csv(Path, as.is=TRUE)
})

# Rbinds the list together
Data <- do.call(rbind,Tables)

文件夹谢谢!第一部分可以完美地从每个文件夹中提取1或11个文件。然后我试着把每一个都读入一个列表,但它不起作用,因为它们是文本文件。我使用了
read.table
,但得到了一个错误。错误是什么?试着拉第一条路径并将其复制到
read.\uuuu()
中,看看是否有效。它可能会给你一个信息更丰富的错误。如果没有关于文件外观的示例,我无法对该部分进行故障排除。文件中的错误(文件,“rt”):无效的“描述”参数您可以将其中一个文件读入R吗?您还可以尝试将行更改为
read.csv(unlist(Path),as.is=T)
。快速搜索这个错误让我觉得路径是以列表而不是字符串的形式传递的,这很有趣。好吧,我只是在我的一个目录上运行了它,但它没有做它应该做的事情。您需要在其中添加
unlist(path)
,以获得所需的内容。(请参见编辑)您希望路径是字符串向量,而不是字符串向量列表。unlist函数修复了这个问题。
Folders <- list.files() # creates a list of all the folders

# Looks in each folder and returns a path to all files starting with 1 or 11.
Paths <- lapply(Folders,function(x){
  F <- list.files(x)
  F <- F[grepl("^(1|11)",F)]
  paste0(x,'/',F)
})
Paths <- unlist(Paths)

# Reads each of the selected files into a list.
Tables <- lapply(Paths, function(Path){
  read.csv(Path, as.is=TRUE)
})

# Rbinds the list together
Data <- do.call(rbind,Tables)