R:如何读取固定宽度的数据文件,其中数据被连接成两组,堆叠在一个文件的顶部

R: How to read fixed width datafile where the data is concatenated into two sets, stacked on top in one file

希望标题有意义。

本质上,一个文件中有两个数据集。

第 1 行包含数据集 1 的标题(按位置)。然后第 2-1500 行是这些位置的条目。

第 1501 行是数据集 2 的标题(按位置)。然后第 1502-3001 行是这些位置的条目。

如何读取具有这些属性的固定文件,为每个数据集(以及数据集 2 的起始点)提供 header 间距。

这里有两种方法:

使用 skipnrows 参数:

first <- read.table("file", header = T, nrows = 1500)
second <- read.table("file", header = T, skip=1501, nrows = 1500)

读入整个文件然后拆分:

allLines <- read.table("file", header = T)
first <- allLines[1:1500, ]
second <- allLines[1502:3002, ]
names(second) <- allLines[1501, ] ## or colnames if working with a matrix