Skip to content

Instantly share code, notes, and snippets.

@chenpanliao
Created October 17, 2014 19:52
Show Gist options
  • Save chenpanliao/db0aa37e2b24863eef85 to your computer and use it in GitHub Desktop.
Save chenpanliao/db0aa37e2b24863eef85 to your computer and use it in GitHub Desktop.
# Copyright 2013 Chen-Pan Liao
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
############################################
## taxaToGuild.r
## Author: Chen-Pan Liao (2013)
## License: Public domain
## Environment: R (ver. 2.15+)
## Usage:
## new.dataframe <- taxaToGuild(
## taxa.file.csv,
## taxa.file.csv.row.num,
## guild.file.csv,
## guild.file.csv.row.num,
## guild.system.row.num
## )
############################################
taxaToGuild <- function (
taxa.file.csv,
taxa.file.csv.row.num,
guild.file.csv,
guild.file.csv.row.num,
guild.system.row.num
# 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
){
guild.data <- read.csv(guild.file.csv, row.names = guild.file.csv.row.num)
taxa.data <- read.csv("taxa.csv", row.names = taxa.file.csv.row.num)
guild.list.taxa <- row.names(guild.data)
guild.list.guild <- guild.data[, guild.system.row.num]
taxa.list <- names(taxa.data)
taxa.list.ind <- numeric(length(taxa.list))
taxa.unknown <- 0
for(n in 1:length(taxa.list)){
if (sum(taxa.list[n] == guild.list.taxa) > 0) {
taxa.list.ind[n] <- which(taxa.list[n] == guild.list.taxa)
} else {
taxa.list.ind[n] <- NA
taxa.unknown[length(taxa.unknown) + 1] <- n
}
}
new.guild.list <- unique(guild.list.guild[taxa.list.ind])
taxa.unknown <- taxa.unknown[-1]
#new.taxa.data <- taxa.data
#names(new.taxa.data) <- guild.list.guild[taxa.list.ind]
## new taxa matrix initiation
newer.taxa.data <- data.frame (
matrix(
0,
nrow = nrow(taxa.data),
ncol = length(new.guild.list)
),
row.names = row.names(taxa.data)
)
names(newer.taxa.data) <- new.guild.list
## subsum of new taxa matrix
for(n in 1:length(new.guild.list)){
if (!is.na(new.guild.list[n])) {
newer.taxa.data[,n] <- rowSums(
data.frame(
taxa.data[
,
which(new.guild.list[n] == guild.list.guild[taxa.list.ind])
]
)
)
} else {
newer.taxa.data[,n] <- rowSums(
data.frame(
taxa.data[ , taxa.unknown]
)
)
}
}
cat(
sprintf(
"The guild system is %s.\n",
names(guild.data)[guild.system.row.num]
)
)
cat(
sprintf(
"Warning: the following taxa is merged into guild NA due to no reference: %s.\n",
taxa.list[taxa.unknown]
)
)
return(newer.taxa.data)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment