【问题标题】:R script for extracting rows from several text files用于从多个文本文件中提取行的 R 脚本
【发布时间】:2016-10-18 17:30:31
【问题描述】:

我的目录中有 900 个文本文件,如下图所示

每个文件都包含以下格式的数据

667869 667869.000000 
580083 580083.000000 
316133 316133.000000 
11065 11065.000000 

我想从每个文本文件中提取第四行并将值存储在一个数组中,欢迎任何建议

【问题讨论】:

    标签: r regression information-extraction


    【解决方案1】:

    这听起来更像是一个 StackOverflow 问题,类似于 Importing multiple .csv files into R

    你可以试试这样的:

    setwd("/path/to/files")

    文件

    头(文件)

    myfiles = lapply(files, function(x) read.csv(file = x, header = TRUE))

    mydata = lapply(myfiles, FUN = function(df){df[4,]}) str(我的数据)

    do.call(rbind, mydata)

    【讨论】:

      【解决方案2】:

      一个懒惰的答案是:

      array <- c()  
      for (file in dir()) {  
        row4 <- read.table(file,
                           header = FALSE,
                           row.names = NULL,
                           skip = 3,  # Skip the 1st 3 rows
                           nrows = 1,  # Read only the next row after skipping the 1st 3 rows
                           sep = "\t")  # change the separator if it is not "\t"  
        array <- cbind(array, row4)
      }
      

      您可以进一步保留文件的名称

      colnames(array) <- dir()
      

      【讨论】:

      • 我想读取多个模式为regional_vol_.txt的文件,所以当我使用文件名regional_vol_.txt时会报错
      猜你喜欢
      • 1970-01-01
      • 2014-11-10
      • 1970-01-01
      • 2023-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多