Вот несколько расширяемый метод.Сначала прочитайте весь файл в переменную с readLines
.Я буду использовать textConnection
для воспроизводимости здесь на SO, но вы должны просто прочитать из файла.
x <- readLines(con=textConnection('
SOURCE
Boxofficemojo.com
STORY
These lines, of variable length and number, would contain the story behind the dataset.
USAGE
"Course" "Year" "Section" "Exercise"
"Course1" 5 9 "ex 3"
"Course1" 5 9 "ex 4"
"Course1" 5 9 "ex 5"
"Course2" 5 9 "ex 3"
"Course2" 5 9 "ex 4"
DATASET
Dataset with headers follows.'))
Отфильтруйте предыдущую пустую строку, которую я ввел:
head(x)
# [1] ""
# [2] "SOURCE"
# [3] "Boxofficemojo.com"
# [4] ""
# [5] "STORY"
# [6] "These lines, of variable length and number, would contain the story behind the dataset."
allcaps <- grep("^[A-Z]+$", x)
if (allcaps[1] > 1) x <- x[-(1:(allcaps[1]-1))]
Я предполагаю, что строка, содержащая только буквы верхнего регистра, указывает на «заголовок».Это также можно сделать с помощью cumsum(x %in% c("USAGE",...))
:
str( x2 <- split(x, cumsum(grepl("^[A-Z]+$", x))) )
# List of 4
# $ 1: chr [1:3] "SOURCE" "Boxofficemojo.com" ""
# $ 2: chr [1:3] "STORY" "These lines, of variable length and number, would contain the story behind the dataset." ""
# $ 3: chr [1:8] "USAGE" "\"Course\" \"Year\" \"Section\" \"Exercise\"" "\"Course1\" 5 9 \"ex 3\"" "\"Course1\" 5 9 \"ex 4\"" ...
# $ 4: chr [1:2] "DATASET" "Dataset with headers follows."
(Вы также можете удалить завершающую пустую строку, возможно, с чем-то вроде x2 <- lapply(x2, head, n=-1)
, хотя последний пострадает, потому что он не 'Это может сработать. Использование Filter(nchar, x2)
также может сработать, но при этом предполагается, что нет «преднамеренных» пустых строк. Вам.)
Этот следующий шаг, возможно, косметический, но делает «заголовок»имя элемента списка с последующими строками и его данными:
str( x3 <- setNames(lapply(x2, `[`, -1L),
sapply(x2, `[`, 1L)) )
# List of 4
# $ SOURCE : chr [1:2] "Boxofficemojo.com" ""
# $ STORY : chr [1:2] "These lines, of variable length and number, would contain the story behind the dataset." ""
# $ USAGE : chr [1:7] "\"Course\" \"Year\" \"Section\" \"Exercise\"" "\"Course1\" 5 9 \"ex 3\"" "\"Course1\" 5 9 \"ex 4\"" "\"Course1\" 5 9 \"ex 5\"" ...
# $ DATASET: chr "Dataset with headers follows."
И, наконец, вы можете делать все, что вам нужно, со встроенными элементами:
x3$USAGE <- read.table(textConnection(x3$USAGE), header=TRUE)
str(x3)
# List of 4
# $ SOURCE : chr [1:2] "Boxofficemojo.com" ""
# $ STORY : chr [1:2] "These lines, of variable length and number, would contain the story behind the dataset." ""
# $ USAGE :'data.frame': 5 obs. of 4 variables:
# ..$ Course : Factor w/ 2 levels "Course1","Course2": 1 1 1 2 2
# ..$ Year : int [1:5] 5 5 5 5 5
# ..$ Section : int [1:5] 9 9 9 9 9
# ..$ Exercise: Factor w/ 3 levels "ex 3","ex 4",..: 1 2 3 1 2
# $ DATASET: chr "Dataset with headers follows."