|
| 1 | + |
| 2 | + |
| 3 | + |
| 4 | +#' Add downsampling index to rppca object |
| 5 | +#' |
| 6 | +#' This index is used by plot.rppca to downsample the col (colour) values. It |
| 7 | +#' is stored in the rppca object's ds slot. |
| 8 | +#' |
| 9 | +#' @param pc an object of class rppca |
| 10 | +#' @param to The down-sampling parameter. A numeric > 0 or a vector or NA. Interpreted |
| 11 | +#' as a proportion or integer or a index vector, see details. |
| 12 | +#' |
| 13 | +#' @details |
| 14 | +#' The parameter `to` is used to specify and possibly which individuals are sampled. |
| 15 | +#' If NA, all individuals are retained. If `to` is of length one and is between 0 and 1, |
| 16 | +#' then it is interpreted as a proportion. If it is greater than 1, it is taken to be |
| 17 | +#' the number of individuals to be sampled (possibly rounded by sample.int). If |
| 18 | +#' `to` is a logical or an integer vector, it is used for logical or integer indexing, respectively. |
| 19 | +#' The integer indices of the sample individuals are written to the `ds` slot. |
| 20 | +#' If `ds` exists, it is overwritten with a warning. |
| 21 | +#' |
| 22 | +#' @return An (invisible) object of class `rppca` with a slot `ds` added. |
| 23 | +#' |
| 24 | +#' @export |
| 25 | +dspc <- function(pc, to=10000){ |
| 26 | + stopifnot(inherits(pc, "rppca")) |
| 27 | + |
| 28 | + nr <- nrow(pc$x) |
| 29 | + |
| 30 | + |
| 31 | + if(length(to)==1){ # If 'to' is a scalar |
| 32 | + if(is.na(to)){ # "sample" all |
| 33 | + pc$ds <- 1:nr |
| 34 | + return(invisible(pc)) |
| 35 | + } |
| 36 | + |
| 37 | + stopifnot(to > 0) |
| 38 | + if(to <1) n <- ceiling(nr * to) else n <- to |
| 39 | + if( n < nr){ |
| 40 | + n <- min(c(n, nr)) |
| 41 | + message(paste0("Downsampling to ", n, " individuals.")) |
| 42 | + ind <- sample.int(nr, n) |
| 43 | + } else { |
| 44 | + ind <- 1:nr |
| 45 | + } |
| 46 | + if(!is.null(pc$ds)) warning("The existing downsampling slot was overwritten.") |
| 47 | + pc$ds <- ind |
| 48 | + return(invisible(pc)) |
| 49 | + } else { # If 'to' is a vector |
| 50 | + pc$ds <- (1:nr)[to] |
| 51 | + message(paste0("Downsampling to ", length(pc$ds), " individuals.")) |
| 52 | + return(invisible(pc)) |
| 53 | + } |
| 54 | + |
| 55 | + stop("Should never reach this point.") |
| 56 | +} |
0 commit comments