R:从多个文件路径导入数据并存储在列表中

时间:2015-08-18 14:38:42

标签: r list import

我想根据各个路径从单独的.txt文件中读取一些数据。可以下载带有txt文件的示例文件夹结构here

(样本)data.frame我看起来像这样

data <-   structure(list(Name = structure(c(1L, 3L, 4L, 2L), .Label = c("Test1", "Test10", "Test2", "Test6"), class = "factor"), Metadata = structure(c(3L, 4L, 1L, 2L), .Label = c("asdajl7", "asfhas", "sgash", "uashas8"), class = "factor"), Filepath = structure(c(2L, 3L, 4L, 1L), .Label = c("", "Folder1/File8.txt", "Folder7/file2.txt", "Folder9/File19.txt"), class = "factor")), .Names = c("Name", "Metadata", "Filepath"), class = "data.frame", row.names = c(NA, -4L))

data
    Name Metadata           Filepath
1  Test1    sgash  Folder1/File8.txt
2  Test2  uashas8  Folder7/file2.txt
3  Test6  asdajl7 Folder9/File19.txt
4 Test10   asfhas

为了制作一个可重复的示例,我尝试实现以下函数,将文件路径调整到上面的下载中保存文件夹结构的位置。

# Choose path to unzipped Data directory
choose.dir <- function() {
  system("osascript -e 'tell app \"R\" to POSIX path of (choose folder with prompt \"Choose Data Folder:\")' > /tmp/R_folder",
         intern = FALSE, ignore.stderr = TRUE)
  p <- system("cat /tmp/R_folder && rm -f /tmp/R_folder", intern = TRUE)
  return(ifelse(length(p), p, NA))
}
a <- choose.dir()
if(is.na(a)) stop("No folder", call. = F)
# paste ready to use path together
data$completepath <- paste0(a,"/",data$Filepath)
data$completepath <- gsub("//", "/", data$completepath)

data.frame现在看起来像这样(我将文件夹结构解压缩到我的桌面):

data
    Name Metadata           Filepath                               completepath
1  Test1    sgash  Folder1/File8.txt  /Users/XYZ/Desktop/Data/Folder1/File8.txt
2  Test2  uashas8  Folder7/file2.txt  /Users/XYZ/Desktop/Data/Folder7/file2.txt
3  Test6  asdajl7 Folder9/File19.txt /Users/XYZ/Desktop/Data/Folder9/File19.txt
4 Test10   asfhas                                      /Users/XYZ/Desktop/Data/

如何使用循环读取来自不同.txt文件的数据,以便获得以下列表结构?

   List with 3 elements
        1.1 (5 observations and 5 Variables)
        $Name chr[1:5] Test1 Test1 Test1 Test1 Test1
        $Year num[1:5] 1783 1784 1785 1786 1787
        $data1 num[1:5] 12 53 13.1 12.9 16
        $data2 num[1:5] 56 5 532 27 9
        $data3 num[1:5] 0.1 9 42 2 13
        1.2 (4 observations and 3 variables)
        $Name chr[1:4] Test2 Test2 Test2 Test2
        $Year num[1:4] 1387 1388 1389 1390
        $data num[1:4] 78.9 27 12.3 0.9
        1.3 (3 observations and 3 variables)
        $Name chr[1:3] Test6 Test6 Test6
        $Test1 chr[1:3] hajshf asfhah ashsa
        $Year num[1:3] 2001 2002 2003

我尝试的是以下内容,但这并不起作用,因为Test10的空文件路径导致了问题。有人能帮助我吗?

# read in the data
f <- file.path(data$completepath)
d <- lapply(f, read.table)

1 个答案:

答案 0 :(得分:4)

你不必这样做,你可以写出路径

setwd("/home/christie/Downloads/Data/")

这将为工作目录中的每个文件提供所有路径

files<-list.files(getwd(),recursive=TRUE)

将它们全部读入列表

d<-lapply(files,function(x) read.table(x, header=T))

str(d)
List of 3
 $ :'data.frame':   5 obs. of  4 variables:
  ..$ data1: num [1:5] 12 53 13.1 12.9 16
  ..$ data2: int [1:5] 56 5 532 27 9
  ..$ data3: num [1:5] 0.1 9 42 2 13
  ..$ year : int [1:5] 1783 1784 1785 1786 1787
 $ :'data.frame':   4 obs. of  2 variables:
  ..$ data: num [1:4] 78.9 27 12.3 0.9
  ..$ year: int [1:4] 1387 1388 1389 1390
 $ :'data.frame':   3 obs. of  3 variables:
  ..$ data1: int [1:3] 18 39 371
  ..$ Test1: Factor w/ 3 levels "asfhah","ashsa",..: 3 1 2
  ..$ Year : int [1:3] 2001 2002 2003