在 R 中如何把分類群資料小計成為功能群資料? 可利用以下介紹的 R function taxaToGuild() 將一分類群資料檔及一分類群對應功能群資料檔轉化出一個功能群資料框. 一般來說, 該 function 也提供一個資料框資料以系統化的方式進行欄小計.
taxaToGuild.R 原始檔
例子
假設有一蜘蛛以科為分類群的物種數資料,以 CSV 檔儲存後如下 (taxa.csv):
"Plot","Leptonetidae","Clubionidae","Others","Araneidae","Oonopidae","Ctenizidae" 1,2,0,0,1,1,2 2,1,2,3,1,0,0 3,1,0,1,1,1,1 4,2,3,2,1,0,1 5,0,1,1,2,1,4 6,2,0,0,0,1,3 7,1,0,1,2,2,1 8,0,0,1,2,3,1
其中第一列皆為變數名且第一欄為可辦視的樣點名。另有一科名對照功能群的資料檔以 CSV 檔儲存後如下 (guild.csv):
"taxa","Guild1","Guild2","Guild3" "Oonopidae","Ground runner","runner","Ground" "Araneidae","Orb weaver","weaver","Space" "Clubionidae","Foliage runner","runner","Foliage" "Ctenidae","Ground runner","runner","Ground" "Ctenizidae","Burrow dweller","dweller","Burrow" "Gnaphosidae","Ground runner","runner","Ground" "Leptonetidae","Ground weaver","weaver","Ground" "Linyphiidae","Space weaver","weaver","Space"
其中第一列皆為變數名且第一欄為可辦視的科名, 第 2 至 4 欄為三套不同的功能群系統. 在將 taxa.csv, guild.csv, taxaToGuild.r 三檔案置於相同某路徑後,於 R 環境中以 setwd("某路徑")
後進行以下動作.
source("taxaToGuild.r") new.dataframe <- taxaToGuild( taxa.file.csv = "taxa.csv", taxa.file.csv.row.num = 1, guild.file.csv = "guild.csv", guild.file.csv.row.num = 1, guild.system.row.num = 1 ) new.dataframe
其中 taxa.file.csv = "taxa.csv"
表示引入 taxa.csv 為分類群資料, taxa.file.csv.row.num = 1
表示 taxa.csv 的第一欄為列名, guild.file.csv = "guild.csv"
表示引入 guild.csv 為功能群對照檔, guild.file.csv.row.num = 1
表示 guild.csv 的第一欄為列名, guild.system.row.num = 1
表示以 guild.csv 的第 1 組對照表 (實際上是此例的第 2 欄). 新產生的功能群資料框賦予變數 new.dataframe
. 操作可得以下結果.
> source("taxaToGuild.r") > new.dataframe <- taxaToGuild( + taxa.file.csv = "taxa.csv", + taxa.file.csv.row.num = 1, + guild.file.csv = "guild.csv", + guild.file.csv.row.num = 1, + guild.system.row.num = 1 + ) The guild system is Guild1. Warning: the following taxa is merged into guild NA due to no reference: Others. > new.dataframe Ground weaver Foliage runner NA Orb weaver Ground runner Burrow dweller 1 2 0 0 1 1 2 2 1 2 3 1 0 0 3 1 0 1 1 1 1 4 2 3 2 1 0 1 5 0 1 1 2 1 4 6 2 0 0 0 1 3 7 1 0 1 2 2 1 8 0 0 1 2 3 1
值得注意的是, 因為 "Others" 無法藉由 "Guild1" 這項對應表成功對應至某個功能群, 所以被丟棄至 new.dataframe 中的 "NA" 欄. 在本例中若欲刪去 new.dataframe 中的 "NA" 欄, 可利用 new.dataframe[,-3]
.