试试这个:
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_rows 或data.table::rbindlist 完成。)
对于 R 来说,实际的列名有点冗长且不标准,我会留给你想出有意义的colnames。