Regex 在R-'中处理字符串;Word格式';列表
除了少数例外,我们可以在Word(.doc)文档中找到物种(尤其是鸟类物种)列表,而且它们的结构形式通常对任何类型的数据分析都是无用的 列表如下所示,包含空格和其他所有内容: 它包括分类学(科)和具有共同学名的物种 数据 像这样的列表是技术报告、地理分布设计、区域保护状况、摘要等的重要信息来源。 这一点对于几乎没有可用或公布的地区尤其重要(上述例子是www.birdsangola.org上安哥拉鸟类名录的一部分)。 如果格式正确,数据将得到更好的使用。数据帧将是对数据进行任何后续分析的良好候选 我想把上面的列表转换成一些有用的东西,提取物种的通用名、学名和分类科。data.frame是一个很好的、自然的候选者。在运行此代码之前,请将上面的数据复制到剪贴板。 R代码: 摘要(针对941个物种的整个数据集)Regex 在R-'中处理字符串;Word格式';列表,regex,r,plyr,Regex,R,Plyr,除了少数例外,我们可以在Word(.doc)文档中找到物种(尤其是鸟类物种)列表,而且它们的结构形式通常对任何类型的数据分析都是无用的 列表如下所示,包含空格和其他所有内容: 它包括分类学(科)和具有共同学名的物种 数据 像这样的列表是技术报告、地理分布设计、区域保护状况、摘要等的重要信息来源。 这一点对于几乎没有可用或公布的地区尤其重要(上述例子是www.birdsangola.org上安哥拉鸟类名录的一部分)。 如果格式正确,数据将得到更好的使用。数据帧将是对数据进行任何后续分析的良好候选
库(plyr)
summary.nesp我看不到这个问题,我建议您重新发布这个问题,在答案部分保留问题并移动答案,以提高可读性
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
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
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