2sb
2sb

Reputation: 649

How to read a table in R with columns separated by unequal spac

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.

Upvotes: 1

Views: 647

Answers (1)

IRTFM
IRTFM

Reputation: 263332

I think the answer (after editing the question to change the data layout to mono-spaced typeface) to the question is read.fwfwhich 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    · ·   

Upvotes: 1

Related Questions