2017-04-15 86 views
0

我正在學習R,我正在嘗試這個數據集。 http://ww2.amstat.org/publications/jse/datasets/airport.dat.txt這個數據集有什麼問題?

不幸的是,使用

ap <- read.table("http://ww2.amstat.org/publications/jse/datasets/airport.dat.txt") 

確實給出了錯誤的結果。該文件是一個「自由格式輸入文件」,如此處所述。 (http://data.princeton.edu/R/readingData.html)。通過在該頁面上給出的例子,我的簡單代碼應該可以工作..但它不會導致虛線和錯誤的條目。怎麼了?

謝謝。

+1

你爲什麼認爲你的代碼應該工作?該文件當然不是read.table的適當格式。 – Roland

+3

這是一個固定寬度的文件。你必須使用'read.fwf'並指定寬度 –

+0

@Roland,或許它很明顯爲什麼它不起作用,但作爲初學者,它不適用於我。我的代碼與我鏈接到的站點中的代碼類似,數據文件的格式也相同。因此,我認爲它應該工作。 – mahela007

回答

1

你必須使用read.fwf並指定widths像這樣:

read.fwf("http://ww2.amstat.org/publications/jse/datasets/airport.dat.txt", 
widths=c(21,21,7,7,9,10,15)) 

         V1     V2  V3  V4  V5  V6  V7 
1 HARTSFIELD INTL  ATLANTA    285693 288803 22665665 165668.76 93039.48 
2 BALTO/WASH INTL  BALTIMORE    73300 74048 4420425 18041.52 19722.93 
3 LOGAN INTL   BOSTON     114153 115524 9549585 127815.09 29785.72 
4 DOUGLAS MUNI   CHARLOTTE    120210 121798 7076954 36242.84 15399.46 
0

閱讀固定寬度的文件始終是一個挑戰,因爲用戶需要搞清楚每列的寬度。爲了完成這個任務,我使用了readr中的函數來簡化過程。

讀取固定寬度文件的主要功能是read_fwf。另外,還有一個叫fwf_empty的功能可以幫助用戶「猜測」列的寬度。但是這個函數並不總能正確識別列的寬度。這是一個例子。

# Load package 
library(readr) 

# Read the data 
filepath <- "http://ww2.amstat.org/publications/jse/datasets/airport.dat.txt" 

# Guess based on position of empty columns 
col_pos <- fwf_empty(filepath) 

# Read the data 
dat <- read_fwf(filepath, col_positions = col_pos) 

# Check the data frame 
head(dat) 

# A tibble: 6 × 6 
       X1       X2  X3  X4  X5  X6 
      <chr>      <chr> <int> <int>  <dbl>  <dbl> 
1 HARTSFIELD INTL ATLANTA    285693 288803 22665665 165668.76 93039.48 
2 BALTO/WASH INTL BALTIMORE    73300 74048 4420425 18041.52 19722.93 
3  LOGAN INTL BOSTON    114153 115524 9549585 127815.09 29785.72 
4 DOUGLAS MUNI CHARLOTTE    120210 121798 7076954 36242.84 15399.46 
5   MIDWAY CHICAGO    64465 66389 3547040 4494.78 4485.58 
6  O'HARE INTL CHICAGO    322430 332338 25636383 300463.80 140359.38 

fwf_empty做了相當不錯的工作,以確定除列2和3。它假定它們是來自同一列中的所有列。所以我們需要一些額外的工作。

fwf_empty的輸出是4個元素的列表,顯示了標識的開始和結束位置,跳過和列名。我們必須更新開始和結束位置以考慮第2列和第3列的存在。

# Extract the begin position 
Begin <- col_pos$begin 

# Extract the end position 
End <- col_pos$end 

# Update the position information 
Begin <- c(Begin[1:2], 43, Begin[3:6]) 
End <- c(End[1], 42, End[2:6]) 

# Update col_pos 
col_pos$begin <- Begin 
col_pos$end <- End 
col_pos$col_names <- paste0("X", 1:7) 

現在我們再次讀取數據。

dat2 <- read_fwf(filepath, col_positions = col_pos) 
head(dat2) 

# A tibble: 6 × 7 
       X1  X2  X3  X4  X5  X6  X7 
      <chr>  <chr> <int> <int> <int>  <dbl>  <dbl> 
1 HARTSFIELD INTL ATLANTA 285693 288803 22665665 165668.76 93039.48 
2 BALTO/WASH INTL BALTIMORE 73300 74048 4420425 18041.52 19722.93 
3  LOGAN INTL BOSTON 114153 115524 9549585 127815.09 29785.72 
4 DOUGLAS MUNI CHARLOTTE 120210 121798 7076954 36242.84 15399.46 
5   MIDWAY CHICAGO 64465 66389 3547040 4494.78 4485.58 
6  O'HARE INTL CHICAGO 322430 332338 25636383 300463.80 140359.38 

這次read_fwf函數可以成功讀取文件。