read.table
with fill=TRUE
can fill them in. The names(DF2)<-
line could be omitted if nice column names are not important. No packages are used.
# test data
Lines <- "pages count
[page 1, page 2, page 3] 23
[page 2, page 4] 4
[page 1, page 3, page 4] 12"
# code - replace text=Lines with something like "myfile.dat"
DF <- read.table(text = Lines, skip = 1, sep = "]", as.is = TRUE)
DF2 <- read.table(text = DF[[1]], sep = ",", fill = TRUE, as.is = TRUE)
names(DF2) <- paste0(read.table(text = Lines, nrow = 1, as.is = TRUE)[[1]], seq_along(DF2))
DF2$count <- DF[[2]]
DF2[[1]] <- sub(".", "", DF2[[1]]) # remove [
which gives this:
> DF2
pages1 pages2 pages3 count
1 page 1 page 2 page 3 23
2 page 2 page 4 4
3 page 1 page 3 page 4 12
Note: This gives columns headings of page1, page2, etc. If it were important to have precisely the column headings shown in the question then replace that line with this which uses those headings if there are less than 20 page columns.
ord <- c('First', 'Second', 'Third', 'Fourth', 'Fifth', 'Sixth', 'Seventh',
'Eighth', 'Ninth', 'Tenth', 'Eleventh', 'Twelfth', 'Thirteenth',
'Fourteenth', 'Fiftheenth', 'Sixteenth', 'Seventeenth', 'Eighteenth',
'Nineteenth')
ix <- seq_along(DF2)
names(DF2) <- if (ncol(DF2) < 20) paste(ord[ix], "Page") else paste("Page", ix)