将ASCII文本表格格式读为R-列表格式

问题描述 投票:0回答:1

我正在尝试将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'

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

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

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

r ascii tabular
1个回答
0
投票

尝试一下:

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

© www.soinside.com 2019 - 2024. All rights reserved.