【问题标题】:Read ASCII text tabular format into R - list format将 ASCII 文本表格格式读入 R - 列表格式
【发布时间】:2020-06-03 19:38:27
【问题描述】:

我正在尝试将 ASCI 文本文件读入 R。但是该格式有点困难,分类变量显示为列表名称,而不是一个大型数据集中的变量。

以下是数据示例:https://cdiac.ess-dive.lbl.gov/ftp/ndp030/nation.1751_2014.ems

这些都不起作用:

url <- 'https://cdiac.ess-dive.lbl.gov/ftp/ndp030/nation.1751_2014.ems'

read.table(url)
scan(url)

我可以skip 到不同的行,但这并不能解决问题。无论我从哪里开始,我都会遇到以下错误:

  • read.table() 抛出关于行号错误的错误 的元素。
  • scan() 到达第一部分时抛出错误 具有不同格式的文件:'expected real got xxx'

我认为应该有一种简单的方法来导入它。显然,如果这种格式本身就很难处理,我将不得不阅读每一行,然后编写一个函数将所有内容分开,那么不用担心。

关于处理这种格式的简单方法有什么想法吗?

附言我意识到这个数据的另一个来源是以 CSV 格式以整洁的格式 (https://cdiac.ess-dive.lbl.gov/trends/emis/tre_coun.html) 存储的。但是我现在想知道如何解决这个问题。

【问题讨论】:

    标签: r ascii tabular


    【解决方案1】:

    试试这个:

    txt <- readLines('https://cdiac.ess-dive.lbl.gov/ftp/ndp030/nation.1751_2014.ems')
    
    ### demonstration of how to find the breaks:
    nms <- grep("^[A-Za-z]+$", txt, value = TRUE)
    head(nms)
    # [1] "AFGHANISTAN" "ALBANIA"     "ALGERIA"     "ANDORRA"     "ANGOLA"     
    # [6] "ANGUILLA"   
    tail(nms)
    # [1] "VANUATU"   "VENEZUELA" "YEMEN"     "ZAMBIA"    "ZANZIBAR"  "ZIMBABWE" 
    

    主要工作:

    lists <- by(txt, cumsum(grepl("^[A-Za-z]+$", txt)), function(s) {
      ind <- grepl("^[0-9]", s)
      if (any(ind)) cbind(cntry = s[1], read.table(text = as.character(s[ind]), stringsAsFactors = FALSE))
    })
    lists <- Filter(length, lists)
    
    head(lists[[1]])
    #         cntry   V1 V2 V3 V4 V5 V6 V7   V8 V9
    # 1 AFGHANISTAN 1949  4  0  0  4  .  0    .  0
    # 2 AFGHANISTAN 1950 23  0 18  6  0  0 0.00  0
    # 3 AFGHANISTAN 1951 25  0 18  7  0  0 0.00  0
    # 4 AFGHANISTAN 1952 25  0 17  9  0  0 0.00  0
    # 5 AFGHANISTAN 1953 29  0 18 10  0  0 0.00  0
    # 6 AFGHANISTAN 1954 29  0 18 12  0  0 0.00  0
    

    将它们全部组合起来:

    alldat <- do.call(rbind, c(lists, list(stringsAsFactors = FALSE)))
    head(alldat)
    #           cntry   V1 V2 V3 V4 V5 V6 V7   V8 V9
    # 1.1 AFGHANISTAN 1949  4  0  0  4  .  0    .  0
    # 1.2 AFGHANISTAN 1950 23  0 18  6  0  0 0.00  0
    # 1.3 AFGHANISTAN 1951 25  0 18  7  0  0 0.00  0
    # 1.4 AFGHANISTAN 1952 25  0 17  9  0  0 0.00  0
    # 1.5 AFGHANISTAN 1953 29  0 18 10  0  0 0.00  0
    # 1.6 AFGHANISTAN 1954 29  0 18 12  0  0 0.00  0
    tail(alldat)
    #           cntry   V1   V2 V3   V4   V5 V6  V7   V8 V9
    # 160.93 ZIMBABWE 2009 1528  0  455  977  0  95 0.11  6
    # 160.94 ZIMBABWE 2010 2121  0  481 1531  0 109 0.15  7
    # 160.95 ZIMBABWE 2011 2608  0  888 1584  0 136 0.18  8
    # 160.96 ZIMBABWE 2012 2125  0 1006  917  0 201 0.15  9
    # 160.97 ZIMBABWE 2013 3184  0 1119 1902  0 162 0.21  9
    # 160.98 ZIMBABWE 2014 3278  0 1005 2097  0 177 0.22  9
    

    (这也可以通过dplyr::bind_rowsdata.table::rbindlist 完成。)

    对于 R 来说,实际的列名有点冗长且不标准,我会留给你想出有意义的colnames

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-27
      • 1970-01-01
      • 1970-01-01
      • 2021-03-25
      • 1970-01-01
      • 1970-01-01
      • 2020-10-21
      • 1970-01-01
      相关资源
      最近更新 更多