該函數已在 readr 中被取代並移至熔化包.
用法
melt_fwf(
  file,
  col_positions,
  locale = default_locale(),
  na = c("", "NA"),
  comment = "",
  trim_ws = TRUE,
  skip = 0,
  n_max = Inf,
  progress = show_progress(),
  skip_empty_rows = FALSE
)
參數
- file
 - 
文件路徑、連接或文字數據(單個字符串或原始向量)。
以
.gz、.bz2、.xz或.zip結尾的文件將自動解壓縮。將自動下載以http://、https://、ftp://或ftps://開頭的文件。遠程gz文件也可以自動下載並解壓。文字數據對於示例和測試最有用。要被識別為文字數據,輸入必須用
I()包裝,是包含至少一個換行符的字符串,或者是至少包含一個帶有換行符的字符串的向量。使用值
clipboard()將從係統剪貼板讀取。 - col_positions
 - 
列位置,由
fwf_empty()、fwf_widths()或fwf_positions()創建。要僅讀入選定的字段,請使用fwf_positions()。如果最後一列的寬度是可變的(參差不齊的 fwf 文件),則將最後一個結束位置提供為 NA。 - locale
 - 
區域設置控製默認值因地而異。默認區域設置為 US-centric(如 R),但您可以使用
locale()創建自己的區域設置來控製默認時區、編碼、小數標記、大標記和日/月名稱等內容。 - na
 - 
要解釋為缺失值的字符串的字符向量。將此選項設置為
character()以指示沒有缺失值。 - comment
 - 
用於標識評論的字符串。注釋字符之後的任何文本都將被默默忽略。
 - trim_ws
 - 
在解析每個字段之前是否應該刪除前導和尾隨空格(ASCII 空格和製表符)?
 - skip
 - 
讀取數據之前要跳過的行數。
 - n_max
 - 
讀取的最大行數。
 - progress
 - 
顯示進度條?默認情況下,它隻會在交互式會話中顯示,而不會在編織文檔時顯示。可以通過將選項
readr.show_progress設置為FALSE來禁用自動進度條。 - skip_empty_rows
 - 
空白行應該被完全忽略嗎?即,如果此選項是
TRUE,則根本不會表示空白行。如果是FALSE,則它們將由所有列中的NA值表示。 
也可以看看
melt_table() 用於融合每列由空格分隔的固定寬度文件,read_fwf() 用於從固定寬度文件讀取矩形數據的常規方法。
例子
fwf_sample <- readr_example("fwf-sample.txt")
cat(read_lines(fwf_sample))
#> John Smith          WA        418-Y11-4111 Mary Hartford       CA        319-Z19-4341 Evan Nolan          IL        219-532-c301
# You can specify column positions in several ways:
# 1. Guess based on position of empty columns
melt_fwf(fwf_sample, fwf_empty(fwf_sample, col_names = c("first", "last", "state", "ssn")))
#> Warning: `melt_fwf()` was deprecated in readr 2.0.0.
#> ℹ Please use `meltr::melt_fwf()` instead
#> # A tibble: 12 × 4
#>      row   col data_type value       
#>    <dbl> <dbl> <chr>     <chr>       
#>  1     1     1 character John        
#>  2     1     2 character Smith       
#>  3     1     3 character WA          
#>  4     1     4 character 418-Y11-4111
#>  5     2     1 character Mary        
#>  6     2     2 character Hartford    
#>  7     2     3 character CA          
#>  8     2     4 character 319-Z19-4341
#>  9     3     1 character Evan        
#> 10     3     2 character Nolan       
#> 11     3     3 character IL          
#> 12     3     4 character 219-532-c301
# 2. A vector of field widths
melt_fwf(fwf_sample, fwf_widths(c(20, 10, 12), c("name", "state", "ssn")))
#> # A tibble: 9 × 4
#>     row   col data_type value        
#>   <dbl> <dbl> <chr>     <chr>        
#> 1     1     1 character John Smith   
#> 2     1     2 character WA           
#> 3     1     3 character 418-Y11-4111 
#> 4     2     1 character Mary Hartford
#> 5     2     2 character CA           
#> 6     2     3 character 319-Z19-4341 
#> 7     3     1 character Evan Nolan   
#> 8     3     2 character IL           
#> 9     3     3 character 219-532-c301 
# 3. Paired vectors of start and end positions
melt_fwf(fwf_sample, fwf_positions(c(1, 30), c(10, 42), c("name", "ssn")))
#> # A tibble: 6 × 4
#>     row   col data_type value       
#>   <dbl> <dbl> <chr>     <chr>       
#> 1     1     1 character John Smith  
#> 2     1     2 character 418-Y11-4111
#> 3     2     1 character Mary Hartf  
#> 4     2     2 character 319-Z19-4341
#> 5     3     1 character Evan Nolan  
#> 6     3     2 character 219-532-c301
# 4. Named arguments with start and end positions
melt_fwf(fwf_sample, fwf_cols(name = c(1, 10), ssn = c(30, 42)))
#> # A tibble: 6 × 4
#>     row   col data_type value       
#>   <dbl> <dbl> <chr>     <chr>       
#> 1     1     1 character John Smith  
#> 2     1     2 character 418-Y11-4111
#> 3     2     1 character Mary Hartf  
#> 4     2     2 character 319-Z19-4341
#> 5     3     1 character Evan Nolan  
#> 6     3     2 character 219-532-c301
# 5. Named arguments with column widths
melt_fwf(fwf_sample, fwf_cols(name = 20, state = 10, ssn = 12))
#> # A tibble: 9 × 4
#>     row   col data_type value        
#>   <dbl> <dbl> <chr>     <chr>        
#> 1     1     1 character John Smith   
#> 2     1     2 character WA           
#> 3     1     3 character 418-Y11-4111 
#> 4     2     1 character Mary Hartford
#> 5     2     2 character CA           
#> 6     2     3 character 319-Z19-4341 
#> 7     3     1 character Evan Nolan   
#> 8     3     2 character IL           
#> 9     3     3 character 219-532-c301 
相關用法
- R readr melt_delim 返回分隔文件中每個標記的熔化數據(包括 csv 和 tsv)
 - R readr melt_table 返回空格分隔文件中每個標記的熔化數據
 - R readr datasource 創建源對象。
 - R readr read_rds 讀/寫 RDS 文件。
 - R readr read_lines 從文件中讀取/寫入行
 - R readr parse_number 靈活地解析數字
 - R readr read_fwf 將固定寬度文件讀入 tibble
 - R readr read_builtin 從包中讀取內置對象
 - R readr Tokenizers 分詞器。
 - R readr date_names 創建或檢索日期名稱
 - R readr type_convert 重新轉換現有 DataFrame 中的字符列
 - R readr locale 創建語言環境
 - R readr write_delim 將數據幀寫入分隔文件
 - R readr parse_vector 解析字符向量。
 - R readr with_edition 暫時更改活動閱讀器版本
 - R readr read_delim 將分隔文件(包括 CSV 和 TSV)讀入 tibble
 - R readr format_delim 將 DataFrame 轉換為分隔字符串
 - R readr edition_get 檢索當前活動版本
 - R readr readr_example 獲取 readr 示例的路徑
 - R readr count_fields 計算文件每一行中的字段數
 - R readr read_table 將空格分隔的列讀入 tibble
 - R readr problems 檢索解析問題
 - R readr parse_guess 使用“最佳”類型進行解析
 - R readr parse_datetime 解析日期/時間
 - R readr read_file 讀/寫完整文件
 
注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Return melted data for each token in a fixed width file。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
