| 1 |
# Copyright (C) Tal Galili |
| 2 |
# |
| 3 |
# This file is part of dendextend. |
| 4 |
# |
| 5 |
# dendextend is free software: you can redistribute it and/or modify it |
| 6 |
# under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 2 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# dendextend is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# A copy of the GNU General Public License is available at |
| 16 |
# http://www.r-project.org/Licenses/ |
| 17 |
# |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#' @title Is the object of some class |
| 22 |
#' @name is_some_class |
| 23 |
#' @description |
| 24 |
#' Returns TRUE if some class (based on the name of the function). |
| 25 |
#' @param x an object. |
| 26 |
#' @return |
| 27 |
#' Returns TRUE if some class (based on the name of the function). |
| 28 |
#' @examples |
| 29 |
#' # TRUE: |
| 30 |
#' is.dendlist(dendlist()) |
| 31 |
#' # FALSE |
| 32 |
#' is.dendlist(1) |
| 33 |
#' # TRUE: |
| 34 |
#' is.dist(dist(mtcars)) |
| 35 |
#' # FALSE |
| 36 |
#' is.dist(mtcars) |
| 37 |
NULL |
| 38 |
|
| 39 |
#' @export |
| 40 |
#' @rdname is_some_class |
| 41 |
is.hclust <- function(x) { |
| 42 |
inherits(x, "hclust") |
| 43 |
} |
| 44 |
|
| 45 |
#' @export |
| 46 |
#' @rdname is_some_class |
| 47 |
is.dendrogram <- function(x) { |
| 48 |
inherits(x, "dendrogram") |
| 49 |
} |
| 50 |
|
| 51 |
#' @export |
| 52 |
#' @rdname is_some_class |
| 53 |
is.phylo <- function(x) { |
| 54 |
inherits(x, "phylo") |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
#' @export |
| 61 |
#' @rdname is_some_class |
| 62 |
is.dendlist <- function(x) { |
| 63 |
inherits(x, "dendlist") |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
#' @export |
| 71 |
#' @rdname is_some_class |
| 72 |
is.dist <- function(x) { |
| 73 |
inherits(x, "dist") |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
#' @title Turns a factor into a number |
| 79 |
#' @export |
| 80 |
#' @description |
| 81 |
#' Turning a factor into a number is not trivial. |
| 82 |
#' Using \code{as.numeric} would only return to us the indicator numbers |
| 83 |
#' and NOT the factor levels turned into a number. |
| 84 |
#' fac2num simply turns a factor into a number, as we often need. |
| 85 |
#' @param x an object. |
| 86 |
#' @param force_integer logical (FALSE). Should the values returned be integers? |
| 87 |
#' @param keep_names logical (TRUE). Should the values returned keep the \link{names} |
| 88 |
#' of the original vector? |
| 89 |
#' @param ... ignored. |
| 90 |
#' @return if x is an object - it returns logical - is the object of class dendrogram. |
| 91 |
#' @examples |
| 92 |
#' |
| 93 |
#' x <- factor(3:5) |
| 94 |
#' as.numeric(x) # 1 2 3 |
| 95 |
#' fac2num(x) # 3 4 5 |
| 96 |
fac2num <- function(x, force_integer = FALSE, keep_names = TRUE, ...) { |
| 97 |
if (!is.factor(x)) stop("x must be a factor (in order to turn it into a number)") |
| 98 |
new_x <- as.numeric(as.character(x)) |
| 99 |
if (force_integer) new_x <- as.integer(new_x) |
| 100 |
if (keep_names) names(new_x) <- names(x) |
| 101 |
return(new_x) |
| 102 |
} |