We assume that each field consist of non-spaces except for field 6 which may have embedded spaces.
Create test file
Lines <- "101 10.08 S A 05OCT93 GOLDEN GATE BRIDGE 4110 6548 6404 55930
101 10.08 S A 05OCT93 GOLDEN GATE BRIDGE 4110 6548 6404 55930
"
cat(Lines, file = "myfile.txt")
Run. Read in the file using readLines
producing L
. Then using gsubfn
in the gsubfn package insert the character defined by sep
between the fields producing g
.
Finally read the text in g
using read.table
to create a data frame:
library(gsubfn)
L <- readLines("myfile.txt")
sep <- ";" # choose any character not in the file
pat <- "(\\S+) (\\S+) (\\S+) (\\S+) (\\S+) (\\S.*\\S) (\\S+) (\\S+) (\\S+) (\\S+)"
pat <- gsub(" ", "\\s+", pat) # can omit if there is only 1 space between fields
g <- gsubfn(pat, ... ~ paste(..., sep = sep), L)
read.table(text = g, sep = sep)
Output. The result of the last line is:
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
1 101 10.08 S A 05OCT93 GOLDEN GATE BRIDGE 4110 6548 6404 1010
2 101 10.08 S A 05OCT93 GOLDEN GATE BRIDGE 4110 6548 6404 1010