【问题标题】:Pick up one column from many files and merge to one matrix in R从多个文件中提取一列并合并到 R 中的一个矩阵
【发布时间】:2011-05-13 04:43:27
【问题描述】:

我有许多带有列(所有文件格式相同)的数据文件(*.dat),我想选取每个文件的一列(所有文件中的相同位置)并将它们合并到一个数据框中.

你能告诉我在 R 中是否可行?怎么做?


我有大约 200 个文件 (23x7)。这是我的数据文件:

  12000.0000       77.17   59.09    0.17        1.82   59.09   32.4564
   6000.0000       77.52   32.68    0.11        1.83   32.68   17.8731
   3000.0000       77.80   18.98    0.12        1.83   18.98   10.3449
   1500.0000       77.99   11.23    0.12        1.84   11.23    6.1084
    750.0000       78.13    6.93    0.15        1.84    6.93    3.7636
    375.0000       78.21    4.53    0.28        1.84    4.53    2.4552
    187.5000       78.27    3.37    0.51        1.85    3.37    1.8253
     93.7500       78.36    2.84    0.99        1.85    2.84    1.5387
     46.8750       78.48    2.04    1.37        1.85    2.04    1.1049
     23.4375       78.53    0.98    0.17        1.85    0.98    0.5291
     11.7188       78.52   -0.23    0.15        1.85   -0.23   -0.1242
      5.8594       78.48   -0.74    0.08        1.85   -0.74   -0.3973
      2.9297       78.44   -0.83    0.03        1.85   -0.83   -0.4499
      1.4648       78.43   -1.49    0.06        1.85   -1.49   -0.8059
      0.7324       78.40   -3.20    0.15        1.85   -3.20   -1.7297
      0.3662       78.24   -5.33    0.04        1.85   -5.33   -2.8879
      0.1831       77.94   -6.84    0.07        1.84   -6.84   -3.7212
      0.0916       77.71   -5.76    0.08        1.83   -5.76   -3.1449
      0.0458       77.35   -3.57    0.11        1.82   -3.57   -1.9588
      0.0229       77.44   -0.88    0.13        1.83   -0.88   -0.4810
      0.0114       77.31    0.72    0.23        1.82    0.72    0.3928
      0.0057       77.59    1.63    0.51        1.83    1.63    0.8929
      0.0029       77.61    0.34    2.65        1.83    0.34    0.1841

我想取出第 6 列并与其他文件中的第 6 列组合以形成一个矩阵 (23x200)。

【问题讨论】:

  • 您应该添加一个文件样本,以便我们知道分隔符是什么,是否有标题,以及如何确保每个文件中的行数相同。
  • 以及所需列的类是什么,因为使用“colClasses”是一种只读取某些列的简洁方法

标签: r dataframe


【解决方案1】:

另一种方法(基于@mdsumner 的回答)是(未经测试):

# get a list of files
my.file.list <- list.files(pattern = "dat$")
# for each file, run read.table and select only the first column
my.list <- lapply(X = my.file.list, FUN = function(x) {
            read.table(x, colClasses = c("NULL", "NULL", "numeric", "NULL"), sep = ",")[,1]
        })
# merge columns that are in a list into one data.frame
my.df <- do.call("cbind", my.list)

【讨论】:

    【解决方案2】:

    如果你想要文件的第三列,它有标题和逗号分隔符:

    d <- read.table("file.dat", colClasses = c("NULL", "NULL", "numeric", "NULL"), sep = ",")
    

    根据需要将“numeric”替换为另一个类 - 以及描述文件中每一列所需的尽可能多的“NULL”。

    获取当前目录中每个名为“*.dat”的文件:

    fs <- list.files(pattern = "dat$")
    

    从所有这些列以及与上述相同的类和列数构建矩阵:

    mat <- NULL
    for (i in 1:length(fs)) {
     mat <- cbind(mat, read.table(fs[i], colClasses = c("NULL", "NULL", "numeric", "NULL"), sep = ",")
    }
    

    对于相当大的数据文件,您应该预先分配矩阵,您可以通过读取一个文件一次性找到(并假设它们都具有相同的行数和结构:

    d0 <- read.table(fs[1], colClasses = c("NULL", "NULL", "numeric", "NULL"), sep = ",")[,1]
    nr <- nrow(d0)
    

    现在上面的循环变得更加节省内存:

    mat <- matrix("numeric", nrow = nr, ncol = length(fs))
    for (i in 1:length(fs)) {
     mat[,i] <- read.table(fs[i], colClasses = c("NULL", "NULL", "numeric", "NULL"), sep = ",")[,1]
    }
    

    【讨论】:

    • 非常感谢,示例文件已添加。
    • colClasses = c("NULL", "NULL", NA, "NULL") 很笼统。
    • 构建矩阵时出现此错误:“data.frame 中的错误(...,check.names = FALSE):参数暗示不同的行数:0、23”。 Hơ 修复它?
    猜你喜欢
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-17
    相关资源
    最近更新 更多