从每个文本文件中提取特定行并存储在一个 txt 文件中

Extracting specific rows from each text files and store in one txt file

   ListOfFileNames= list.files(path = "D:/in/",
                       pattern = '*.txt',recursive = T)
   options(stringsAsFactors = F)
   setwd("D:/in/")
   outFile <- file("output.txt", "w")
   for (i in ListOfFileNames){
     x = read.delim(ListOfFileNames[i], skip = 29, nrows= 1)
     x = as.character(x)
    writeLines(x, paste('D:/out/out.csv',sep = ","))

   }

enter link description hereThis the txt files that I have.

我想从每个 txt 文件中提取行号 30 和 63 并将其保存到一个 txt 文件中。我怎样才能在 R 中解决这个问题?这是我尝试提取行号 30 并将其存储在一个 csv 文件中的代码。但它不起作用。你能帮忙吗?

谢谢

你可以试试:

ListOfFileNames= list.files(path = "D:/in/",
                       pattern = '*.txt',recursive = TRUE, full.names = TRUE)

result <- do.call(rbind, lapply(ListOfFileNames, function(x) 
                  read.csv(x)[c(30, 63), ]))

write.csv(result, 'D:/out/out.csv', row.names = FALSE)