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

Go To StackoverFlow.com

1

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.

2012-04-05 19:28
by 2sb
The default separator for 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
I haven't seen this problem since the days of Hollerith cards. Anyway, please provide: the exact type of file you are trying to read from, and what you mean by "column." You said you wanted to split one column into 4, but then went sideways into a multi-column set of data - Carl Witthoft 2012-04-05 19:51


1

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    · ·   
2012-04-05 19:55
by 42-
Thanks for editing 2 - 2sb 2012-04-05 20:11
Ads