This is one column of a data frame. I want to further split into for 4 columns. The problem is the spaces between each columns which is indifferent depending upon the numbers.
-92 -100 0 29
·· ·· 0 29
0 0 0 0
-- -- -- --
-93 21 ·· ··
1st Row // There is 1 space between -92 and -100 and 4 spaces between 100 and 0 and 3 space between 0 and 29.
2nd Row There is 3 space between 1st col and 2nd col, 3 space between 2nd and 3rd column and 3 space between 3rd and 4th column
3rd Row
4 space between each column
4th Row
3 space between each column.
I think the answer (after editing the question to change the data layout to mono-spaced typeface) to the question is read.fwf
which is in the 'utils' package so it should be available without needing to load anything.
read.fwf(file=textConnection(" -92 -100 0 29
·· ·· 0 29
0 0 0 0
-- -- -- --
-93 21 ·· ··"), header=FALSE, widths=c(4,5,4,4))
#------------------
V1 V2 V3 V4
1 -92 -100 0
2 ·· ·· 0
3 0 0 0
4 -- -- - -
5 -93 21 · ·
read.table()
is""
, which from the help page says "Values on each line of the file are separated by this character. If sep = "" (the default for read.table) the separator is ‘white space’, that is one or more spaces, tabs, newlines or carriage returns.` The key there is "one or more spaces, tabs, ...". Does that work for your application - Chase 2012-04-05 19:46