【发布时间】:2016-10-18 17:30:31
【问题描述】:
我的目录中有 900 个文本文件,如下图所示
每个文件都包含以下格式的数据
667869 667869.000000
580083 580083.000000
316133 316133.000000
11065 11065.000000
我想从每个文本文件中提取第四行并将值存储在一个数组中,欢迎任何建议
【问题讨论】:
标签: r regression information-extraction
我的目录中有 900 个文本文件,如下图所示
每个文件都包含以下格式的数据
667869 667869.000000
580083 580083.000000
316133 316133.000000
11065 11065.000000
我想从每个文本文件中提取第四行并将值存储在一个数组中,欢迎任何建议
【问题讨论】:
标签: r regression information-extraction
这听起来更像是一个 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)
【讨论】:
一个懒惰的答案是:
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()
【讨论】: