該函數已在 readr 中被取代並移至熔化包.
對於某些非矩形數據格式,將數據解析為融合格式(其中每行代表一個標記)可能很有用。
melt_table() 和 melt_table2() 旨在讀取每列由一列(或多列)空格分隔的文本數據類型。
melt_table2() 允許列之間有任意數量的空白字符,並且行可以有不同的長度。
melt_table() 更嚴格,每一行的長度必須相同,並且每行中每個字段的位置相同。它首先找到空列,然後像固定寬度文件一樣進行解析。
用法
melt_table(
  file,
  locale = default_locale(),
  na = "NA",
  skip = 0,
  n_max = Inf,
  guess_max = min(n_max, 1000),
  progress = show_progress(),
  comment = "",
  skip_empty_rows = FALSE
)
melt_table2(
  file,
  locale = default_locale(),
  na = "NA",
  skip = 0,
  n_max = Inf,
  progress = show_progress(),
  comment = "",
  skip_empty_rows = FALSE
)
參數
- file
 - 
文件路徑、連接或文字數據(單個字符串或原始向量)。
以
.gz、.bz2、.xz或.zip結尾的文件將自動解壓縮。將自動下載以http://、https://、ftp://或ftps://開頭的文件。遠程gz文件也可以自動下載並解壓。文字數據對於示例和測試最有用。要被識別為文字數據,輸入必須用
I()包裝,是包含至少一個換行符的字符串,或者是至少包含一個帶有換行符的字符串的向量。使用值
clipboard()將從係統剪貼板讀取。 - locale
 - 
區域設置控製默認值因地而異。默認區域設置為 US-centric(如 R),但您可以使用
locale()創建自己的區域設置來控製默認時區、編碼、小數標記、大標記和日/月名稱等內容。 - na
 - 
要解釋為缺失值的字符串的字符向量。將此選項設置為
character()以指示沒有缺失值。 - skip
 - 
讀取數據之前要跳過的行數。
 - n_max
 - 
讀取的最大行數。
 - guess_max
 - 
用於猜測列類型的最大行數。永遠不會使用超過讀取的行數。有關更多詳細信息,請參閱
vignette("column-types", package = "readr")。 - progress
 - 
顯示進度條?默認情況下,它隻會在交互式會話中顯示,而不會在編織文檔時顯示。可以通過將選項
readr.show_progress設置為FALSE來禁用自動進度條。 - comment
 - 
用於標識評論的字符串。注釋字符之後的任何文本都將被默默忽略。
 - skip_empty_rows
 - 
空白行應該被完全忽略嗎?即,如果此選項是
TRUE,則根本不會表示空白行。如果是FALSE,則它們將由所有列中的NA值表示。 
也可以看看
melt_fwf() 融合固定寬度文件,其中每列不由空格分隔。 melt_fwf() 對於讀取非標準格式的表格數據也很有用。 read_table() 是從空格分隔的文件中讀取表格數據的傳統方法。
例子
fwf <- readr_example("fwf-sample.txt")
writeLines(read_lines(fwf))
#> John Smith          WA        418-Y11-4111
#> Mary Hartford       CA        319-Z19-4341
#> Evan Nolan          IL        219-532-c301
melt_table(fwf)
#> Warning: `melt_table()` was deprecated in readr 2.0.0.
#> ℹ Please use `meltr::melt_table()` 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
ws <- readr_example("whitespace-sample.txt")
writeLines(read_lines(ws))
#> first last state phone
#> John Smith WA 418-Y11-4111
#> Mary Hartford CA 319-Z19-4341
#> Evan Nolan IL 219-532-c301
melt_table2(ws)
#> Warning: `melt_table2()` was deprecated in readr 2.0.0.
#> ℹ Please use `meltr::melt_table2()` instead
#> # A tibble: 16 × 4
#>      row   col data_type value       
#>    <dbl> <dbl> <chr>     <chr>       
#>  1     1     1 character first       
#>  2     1     2 character last        
#>  3     1     3 character state       
#>  4     1     4 character phone       
#>  5     2     1 character John        
#>  6     2     2 character Smith       
#>  7     2     3 character WA          
#>  8     2     4 character 418-Y11-4111
#>  9     3     1 character Mary        
#> 10     3     2 character Hartford    
#> 11     3     3 character CA          
#> 12     3     4 character 319-Z19-4341
#> 13     4     1 character Evan        
#> 14     4     2 character Nolan       
#> 15     4     3 character IL          
#> 16     4     4 character 219-532-c301
相關用法
- R readr melt_delim 返回分隔文件中每個標記的熔化數據(包括 csv 和 tsv)
 - R readr melt_fwf 返回固定寬度文件中每個標記的熔化數據
 - 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 whitespace-separated file。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
