Skip to content

Instantly share code, notes, and snippets.

@uribo
Last active August 29, 2015 14:05
Show Gist options
  • Save uribo/d7e8e05d3d3de6bf7b0b to your computer and use it in GitHub Desktop.
Save uribo/d7e8e05d3d3de6bf7b0b to your computer and use it in GitHub Desktop.
学名の属名と種小名から略称を生成する関数
library(stringr)
# お試しデータフレームの作成
df <- data.frame(
Species = c("Quercus serrata", "Quercus acuta", "Fagus crenata", "Viburnum odoratissimum var. awabuki"),
DBH = c(30, 35, 37, 18))
# 関数の定義
get.abb <- function (species, genus.max = 2, spsn.max = 2, sep = "."){
species <- as.character(species)
abb <- paste(
substr(species, 1, genus.max),
substr(str_split_fixed(species, " ", 10)[, 2], 1, spsn.max),
sep = sep)
# duplicated(abb)
return(abb)
}
# 関数の実行
get.abb(df$Species)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment