R脚本,用于从多个文本文件中提取行

时间:2016-10-18 17:30:31

标签: r regression information-extraction

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

enter image description here

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

667869 667869.000000 
580083 580083.000000 
316133 316133.000000 
11065 11065.000000 

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

2 个答案:

答案 0 :(得分:0)

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

您可以尝试以下内容:

  

setwd( “/路径/到/文件”)

     

files< - list.files(path = getwd(),recursive = FALSE)

     

头(文件)

     

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

     

mydata = lapply(myfiles,FUN = function(df){df [4,]})      STR(MYDATA)

     

do.call(rbind,mydata)

答案 1 :(得分:0)

懒惰的回答是:

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