-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathmake.cepnames.R
37 lines (37 loc) · 1.48 KB
/
make.cepnames.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
`make.cepnames` <-
function (names, minlengths = c(4,4), seconditem = FALSE,
uniqgenera = FALSE, named = FALSE, method)
{
if (named)
orignames <- names
## do not split by hyphens, but collapse hyphened names
names <- gsub("-", "", names)
## make valid names
names <- make.names(names, unique = FALSE, allow_ = FALSE)
## remove trailing and duplicated dots
names <- gsub("\\.[\\.]+", ".", names)
names <- gsub("\\.$", "", names)
## split by dots and get genus and epithet
names <- strsplit(names, ".", fixed = TRUE)
gen <- sapply(names, function(x) x[1])
epi <- sapply(names,
function(x) {if (seconditem) x[2]
else if (length(x) > 1) x[length(x)] else ""})
## strict=TRUE always takes given minlength even if these are duplicates
glen <- minlengths[1]
nmlen <- sum(minlengths)
if (missing(method))
method <- "left.kept"
gen <- ifelse(epi != "",
abbreviate(abbreviate(gen, glen, use.classes = FALSE,
strict = !uniqgenera),
glen, use.classes = TRUE, method = method),
gen)
names <- abbreviate(paste0(gen, epi), nmlen, use.classes = FALSE)
## try to remove wovels if names > nmlen
names <- abbreviate(names, nmlen, use.classes = TRUE, method = method,
named = FALSE)
if (named)
names(names) <- orignames
names
}