Хорошо, я справился, так как заметил 3 вещи: (1) первый столбец описывает, что содержит каждая строка; (2) первая строка каждой таблицы описывает, что содержит каждый столбец этой таблицы и начинается со слова TYPE; и (3) строка после каждой таблицы содержит только * в первом столбце, за исключением последней таблицы, которая не имеет ничего после нее. Я добавил строку в конце с *, чтобы каждая таблица следовала одному и тому же шаблону, и поэтому я мог получить правильные индексы.
Код обходного пути, измененный для набора тестовых данных (он дает те же результаты):
#Step 1: Read full data set
tables.df <- read.table("tablesTest2SampleDataSet.txt", header=FALSE, fill = TRUE, stringsAsFactors = FALSE)
#Append a row that starts with an * to the end of the file
tables.df <- rbind(tables.df, c("*"))
#Step 2: Establish identifier for the start and ending of each table in the data set
#Gets row names of the rows that start with the name TYPE
typeRows <- which(tables.df$V1 == "TYPE")
#Gets row names of the rows that start with *
starRows <- which(tables.df$V1 == "*")
#Gets column names of the slots in the TYPE rows that are empty
#Therefore i can use the first item in each of these to get the last column with data
for (i in 1:length(typeRows))
{
assign(paste("emptyColumnsT", i, sep = ""), which(tables.df[typeRows[i],] == ""))
}
#Step 3: Create the tables
for (i in 1:length(typeRows))#One table per typeRows value
{
if(length(get(paste("emptyColumnsT", i, sep = ""))) == 0)
{
#New frame with length = to original and height = to space between typeRows
#and starRows/end of file.
istar <- starRows[i]-1
#If I use starRows[i]-1 instead of istar in the
#statement below it doesn't divide the table properly
assign(paste("tables.df_table", i, sep = ""), tables.df[c(typeRows[i]:
istar),c(1:length(tables.df))])
}else
{
#New frame with length = one slot prior to the first value of each emptyColumnT
#and height = to space between typeRows and starRows/end of file.
istar <- starRows[i]-1
#If I use starRows[i]-1 instead of istar in the
#statement below it doesn't divide the table properly
assign(paste("tables.df_table", i, sep = ""), tables.df[c(typeRows[i]:
istar),c(1:get(paste("emptyColumnsT", i, sep = ""))[1]-1)])
}
}
Вот пример набора данных, который я использовал для этого теста:
TYPE text bool num num
HEADERS HEAD1 HEAD2 HEAD3 HEAD4
DATA abcd 1 123 456
*
TYPE text num num num num num num num num bool
HEADERS2 HT1 HN1 HN2 HN3 HN4 HN5 HN6 HN7 HN8 HB
DATA efgh 789 098 765 432 112 358 132 134 0
*
TYPE text text text num num num
HEADERS3 H1 H2 H3 H4 H5 H6
DATA ijkl mnop qrst 558 914 400
В конце я хочу разделить файл на столько таблиц, сколько в нем содержится; в этом случае 3. Строки каждой из таблиц должны начинаться в строке ТИПА и заканчиваться строкой перед строкой *. Что касается столбца, у каждого не должно быть пустых слотов в конце. Поэтому все 3 таблицы в этом тесте имеют разную длину.