refactor biCode to bring it in line with current coding standards
This commit is contained in:
parent
9ac45565f4
commit
4fb2506e58
24
.utilities.R
24
.utilities.R
@ -50,19 +50,21 @@ objectInfo <- function(x) {
|
|||||||
|
|
||||||
|
|
||||||
biCode <- function(s) {
|
biCode <- function(s) {
|
||||||
# make a 5 character code from a binomial name by concatening
|
# make a 5 character "biCode" from a binomial name by concatening
|
||||||
# the first three letter of the first word and the first
|
# the uppercased first three letter of the first word and the first
|
||||||
# two letters of the second word.
|
# two letters of the second word.
|
||||||
#
|
# s: character vector of binomial species names
|
||||||
# There are many ways to do this, here we assemble the two parts
|
# value: character vector of biCodes, same length as s
|
||||||
# in a loop, this way the function is vectorized and can
|
|
||||||
# work on a large vector of names.
|
b <- character(length(s))
|
||||||
b <- character()
|
s <- gsub("[^a-zA-Z ]", "", s) # remove all non-alphabetic characters
|
||||||
for (i in 1:length(s)) {
|
# except space
|
||||||
|
s <- toupper(s)
|
||||||
|
|
||||||
|
for (i in seq_along(s)) {
|
||||||
b[i] <- sprintf("%s%s",
|
b[i] <- sprintf("%s%s",
|
||||||
toupper(unlist(substr(s[i], 1, 3))),
|
unlist(substr(s[i], 1, 3)),
|
||||||
toupper(unlist(substr(strsplit(s[i], "\\s+")[[1]][2],
|
unlist(substr(strsplit(s[i], "\\s+")[[1]][2], 1, 2)))
|
||||||
1, 2))))
|
|
||||||
}
|
}
|
||||||
return(b)
|
return(b)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user