|
a |
|
b/R/remove.low.cv.R |
|
|
1 |
#' Remove features with low variation |
|
|
2 |
#' |
|
|
3 |
#' |
|
|
4 |
#' \code{remove.low.cv} that removes variables with low variation. |
|
|
5 |
#' From a matrix/data.frame (samples in rows, features in columns), it computes the coefficient of variation for every features (columns) |
|
|
6 |
#' and return a filtered data.frame with features for which the coefficient of variation is above a given threshold. |
|
|
7 |
#' |
|
|
8 |
#' @param X a matrix/data.frame |
|
|
9 |
#' @param cutoff a numeric value |
|
|
10 |
#' |
|
|
11 |
#' @return |
|
|
12 |
#' a data.frame/matrix |
|
|
13 |
#' |
|
|
14 |
#' @examples |
|
|
15 |
#' mat <- matrix(sample(1:3, size = 200, replace = TRUE), ncol=20) |
|
|
16 |
#' remove.low.cv(mat, 0.4) |
|
|
17 |
#' |
|
|
18 |
#' @export |
|
|
19 |
remove.low.cv <- function(X, cutoff = 0.5){ |
|
|
20 |
stopifnot(is(X, "data.frame") | is(X, "matrix")) |
|
|
21 |
stopifnot(is.vector(cutoff) & is.numeric(cutoff) & (length(cutoff) == 1)) |
|
|
22 |
|
|
|
23 |
# var.coef |
|
|
24 |
cv <- unlist(lapply(as.data.frame(X), |
|
|
25 |
function(x) abs(sd(x)/mean(x)))) |
|
|
26 |
return(X[,cv > cutoff]) |
|
|
27 |
} |