Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
584 views
in Technique[技术] by (71.8m points)

read.table - Read observations in fixed width files spanning multiple lines in R

I am trying to read a dat file in R. The data is available here.

The specification about the dataset is available here.

The first part of my problem is solved by using the sep option and providing information about where every column starts and providing the na.strings="*". However, I do not know how to handle a single observation spanning more than 1 line.

In this data set, all the observations span 2 lines.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You actually need read.fwf for this.

Set up some sample data

    txt <- 'Acura         Integra        Small   12.9 15.9 18.8 25 31 0 1 4 1.8 140 6300
2890 1 13.2 5 177 102 68 37 26.5 11 2705 0
Acura         Legend         Midsize 29.2 33.9 38.7 18 25 2 1 6 3.2 200 5500
2335 1 18.0 5 195 115 71 38 30.0 15 3560 0
Audi          90             Compact 25.9 29.1 32.3 20 26 1 1 6 2.8 172 5500
2280 1 16.9 5 180 102 67 37 28.0 14 3375 0'

Read using read.fwf - pay attention to widths argument. The widths should be a list of 2 integer vectors specifying element widths on multiple lines

DF <- read.fwf(textConnection(txt), 
               widths = list(
                 c(14, 15, 8, 5, 5, 5, 3, 3, 2, 2, 2, 4, 4, 4), 
                 c(5, 2, 5, 2, 4, 4, 3, 3, 5, 3, 5, 1)
               ), 
               header = FALSE)

Using pander package to pretty print the table since it has so many columns.

require(pander)
pandoc.table(DF)
## 
## ---------------------------------------------------
##  V1     V2      V3     V4   V5   V6   V7   V8   V9 
## ----- ------- ------- ---- ---- ---- ---- ---- ----
## Acura Integra  Small  12.9 15.9 18.8  25   31   0  
## 
## Acura Legend  Midsize 29.2 33.9 38.7  18   25   2  
## 
## Audi    90    Compact 25.9 29.1 32.3  20   26   1  
## ---------------------------------------------------
## 
## Table: Table continues below
## 
##  
## -----------------------------------------------
##  V10   V11   V12   V13   V14   V15   V16   V17 
## ----- ----- ----- ----- ----- ----- ----- -----
##   1     4    1.8   140  6300  2890    1   13.2 
## 
##   1     6    3.2   200  5500  2335    1   18.0 
## 
##   1     6    2.8   172  5500  2280    1   16.9 
## -----------------------------------------------
## 
## Table: Table continues below
## 
##  
## -----------------------------------------------
##  V18   V19   V20   V21   V22   V23   V24   V25 
## ----- ----- ----- ----- ----- ----- ----- -----
##   5    177   102   68    37   26.5   11   2705 
## 
##   5    195   115   71    38   30.0   15   3560 
## 
##   5    180   102   67    37   28.0   14   3375 
## -----------------------------------------------
## 
## Table: Table continues below
## 
##  
## -----
##  V26 
## -----
##   0  
## 
##   0  
## 
##   0  
## -----
## 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...