Search code examples
rseparatorread.table

R: read.table seperator every XX characters


Unable to find the solution to my problem.

I am trying to read a text file using r. File contains a single row and separated by number of characters.

000341656.0000000000000000004.6000000000000000009.0000000000000000050.9566787004000000052.0000000000000000072.8621215573000000007.0000000000000000050.0361010830000000047.2490974729000000054.5560183531000000006.0000000000000000049.9711191336000000047.0397111913000000043.1488475260000000023.0000000000000000046.6281588448000000040.1516245487000000038.4653540241000000002.0000000000000000046.2129963899000000041.9963898917000000037.3850068798000000030.0000000000000000046.0144404332000000040.0324909747000000027.0930952140000000003.0000000000000000043.3971119134000000032.4801444043000000010.4757238771

First value is of 20 digit floating.9 digit followed by 10 decimal digit.

The file contains between 22 and 30 values, each 20 digits long (decimal digit set to '.')

What i am unable to figure out how to get rid of this extra 0.

Any lead of help is highly appreciable.


Solution

  • You can read data in a fixed-width format with read.fwf:

    > read.fwf("./d.txt", widths=rep(20,30))
          V1  V2 V3       V4 V5       V6 V7      V8      V9      V10 V11      V12
    1 341656 4.6  9 50.95668 52 72.86212  7 50.0361 47.2491 54.55602   6 49.97112
    2 341656 4.6  9 50.95668 52 72.86212  7 50.0361 47.2491 54.55602   6 49.97112
           V13      V14 V15      V16      V17      V18 V19    V20      V21      V22
    1 47.03971 43.14885  23 46.62816 40.15162 38.46535   2 46.213 41.99639 37.38501
    2 47.03971 43.14885  23 46.62816 40.15162 38.46535   2 46.213 41.99639 37.38501
      V23      V24      V25     V26 V27      V28      V29      V30
    1  30 46.01444 40.03249 27.0931   3 43.39711 32.48014 10.47572
    2  30 46.01444 40.03249 27.0931   3 43.39711 32.48014 10.47572
    

    You need to know how many fields and how big they are. You didnt say how many lines in the file but I copied your line in twice (hence the duplication).