0

除了少数例外,人们会在 Word (.doc) 文档中找到物种列表(尤其是鸟类),而且它们的结构通常以一种对任何类型的数据分析都无用的方式进行构建。

列表将是这样的,带有空格和其他所有内容:它包括分类(科)和具有通用和科学名称的物种。

数据

1 STRUTHIONIDAE (1)
Common Ostrich Struthio camelus


2 DIOMEDEIDAE (5 – 1 + 1)

++Northern Royal Albatross Diomedea sanfordi

Black-browed Albatross Thalassarche melanophris

Shy Albatross Thalassarche cauta

Grey-headed Albatross Thalassarche chrysostoma

Atlantic Yellow-nosed Albatross Thalassarche chlororhynchos


3 Procellaridae (11 – 1 + 1)

Southern Giant Petrel Macronectes giganteus

Pintado Petrel Daption capense

Great-winged Petrel Pterodroma macroptera

Soft-plumaged Petrel Pterodroma mollis

Antarctic Prion Pachyptila desolata

White-chinned Petrel Procellaria aequinoctialis

++Spectacled Petrel Procellaria conspicillata

Cory's Shearwater Calonectris [diomedea] borealis

Great Shearwater Puffinus gravis

Sooty Shearwater Puffinus griseus

Manx Shearwater Puffinus puffinus


4 HYDROBATIDAE (3)

Wilson's Storm-Petrel Oceanites oceanicus

British Storm-Petrel Hydrobates pelagicus

Leach's Storm-Petrel Oceanodroma leucorhoa

像这样的列表是技术报告、地理分布设计、区域保护状况、摘要等信息的非凡来源。这对于很少可用或发表的地区特别感兴趣(上面的例子是鸟类列表的一部分来自 www.birdsangola.org 的安哥拉)。如果格式正确,数据将得到更好的使用。数据框将是对数据进行任何后续分析的良好候选者。

我想将上面的列表转换为可用的东西,提取物种通用名、学名和分类科。data.frame 将是一个很好的、自然的候选者。

4

1 回答 1

0

在运行此代码之前,将上面的数据复制到剪贴板。

代码:

library(stringr)
# Read from clipboard (blank.lines.skip = T)
orig.list <- read.delim2('clipboard', header = F, stringsAsFactors = F)
l.species <- data.frame()
for(i in 1:nrow(orig.list)) {
  tmp.string <- unlist(str_extract_all(orig.list[i, ], "[A-Za-z]+"))
  l.species[i, 1] <- ifelse(length(tmp.string) == 1, tmp.string,
                            paste(tmp.string[1:(length(tmp.string)-2)],
                                  collapse = ' '))
  l.species[i, 2] <- paste(tmp.string[(length(tmp.string) - 1) : length(tmp.string)],
                           collapse = ' ')
  l.species[i, 3]<-ifelse(length(tmp.string) == 1, 1, 0)
}
names(l.species) <- c('common name', 'species', 'is.family')
taxon.family <- toupper(subset(l.species, is.family == 1,
                               select = species)$species)
rows.family <- as.numeric(row.names(subset(l.species, is.family == 1)))
l.species$family <- rep(taxon.family, times = diff(c(rows.family,
                                                     nrow(l.species)+1)))
l.spec.family <- subset(l.species, is.family == 0, select = -is.family) 

结果对象:

> head(l.spec.family)                            
                      common name                     species        family
2                  Common Ostrich            Struthio camelus STRUTHIONIDAE
4        Northern Royal Albatross           Diomedea sanfordi   DIOMEDEIDAE
5          Black browed Albatross    Thalassarche melanophris   DIOMEDEIDAE
6                   Shy Albatross          Thalassarche cauta   DIOMEDEIDAE
7           Grey headed Albatross    Thalassarche chrysostoma   DIOMEDEIDAE
8 Atlantic Yellow nosed Albatross Thalassarche chlororhynchos   DIOMEDEIDAE

总结(包含 941 个物种的整个数据集)

library(plyr)
summary.nesp <- ddply(l.spec.family, .(family), summarise,
                      prop_esp = length(family)/nrow(*all.data*)*100)
top.summary.nesp <- head(summary.nesp[order(summary.nesp$prop_esp, decreasing = T),], 6)

> top.summary.nesp
          family prop_esp
79     SYLVIIDAE 8.076514
1   ACCIPITRIDAE 5.419766
48    PASSERIDAE 5.100956
24   ESTRILDIDAE 4.250797
83      TURDIDAE 3.613177
44 NECTARINIIDAE 3.506908
于 2012-08-29T13:32:06.673 回答