R version 4.4.0 beta (2024-04-09 r86391 ucrt) -- "Puppy Cup" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library("matrixStats") > > ## Create isFALSE() if running on an old version of R > if (!exists("isFALSE", mode="function")) { + isFALSE <- function(x) is.logical(x) && length(x) == 1L && !is.na(x) && !x + } > > rowWeightedMedians_R <- function(x, w, na.rm = FALSE, ..., useNames = NA) { + res <- apply(x, MARGIN = 1L, FUN = weightedMedian, w = w, na.rm = na.rm, ...) + + # Keep naming support consistency same as rowWeightedMedians() + if (!is.null(w)) { + if (isFALSE(useNames)) names(res) <- NULL + } + else if (is.na(useNames) || !useNames) names(res) <- NULL + + res + } > > colWeightedMedians_R <- function(x, w, na.rm = FALSE, ..., useNames = NA) { + res <- apply(x, MARGIN = 2L, FUN = weightedMedian, w = w, na.rm = na.rm, ...) + + # Keep naming support consistency same as colWeightedMedians() + if (!is.null(w)) { + if (isFALSE(useNames)) names(res) <- NULL + } + else if (is.na(useNames) || !useNames) names(res) <- NULL + + res + } > > set.seed(1) > > x <- matrix(rnorm(20), nrow = 5, ncol = 4) > print(x) [,1] [,2] [,3] [,4] [1,] -0.6264538 -0.8204684 1.5117812 -0.04493361 [2,] 0.1836433 0.4874291 0.3898432 -0.01619026 [3,] -0.8356286 0.7383247 -0.6212406 0.94383621 [4,] 1.5952808 0.5757814 -2.2146999 0.82122120 [5,] 0.3295078 -0.3053884 1.1249309 0.59390132 > > # To check names attribute > dimnames <- list(letters[1:5], LETTERS[1:4]) > > # Non-weighted row medians > x_est0 <- rowMedians(x) > x_est1 <- rowWeightedMedians(x) > stopifnot(all.equal(x_est1, x_est0)) > x_est2 <- colWeightedMedians(t(x)) > stopifnot(all.equal(x_est2, x_est0)) > # Test with and without dimnames on x > for (setDimnames in c(TRUE, FALSE)) { + if (setDimnames) dimnames(x) <- dimnames + else dimnames(x) <- NULL + # Check names attribute + for (useNames in c(if (!matrixStats:::isUseNamesNADefunct()) NA, TRUE, FALSE)) { + x_est0 <- rowMedians(x, useNames = useNames) + x_est1 <- rowWeightedMedians(x, useNames = useNames) + x_est2 <- colWeightedMedians(t(x), useNames = useNames) + stopifnot(all.equal(x_est1, x_est0)) + stopifnot(all.equal(x_est2, x_est0)) + } + } > > > # Weighted row medians (uniform weights) > w <- rep(2.5, times = ncol(x)) > x_est0 <- rowMedians(x) > x_est1 <- rowWeightedMedians(x, w = w) > stopifnot(all.equal(x_est1, x_est0)) > x_est2 <- colWeightedMedians(t(x), w = w) > stopifnot(all.equal(x_est2, x_est0)) > # Test with and without dimnames on x > for (setDimnames in c(TRUE, FALSE)) { + if (setDimnames) dimnames(x) <- dimnames + else dimnames(x) <- NULL + # Check names attribute + for (useNames in c(if (!matrixStats:::isUseNamesNADefunct()) NA, TRUE, FALSE)) { + x_est0 <- rowWeightedMedians_R(x, w = w, useNames = useNames) + x_est1 <- rowWeightedMedians(x, w = w, useNames = useNames) + stopifnot(all.equal(x_est1, x_est0)) + x_est0 <- colWeightedMedians_R(t(x), w = w, useNames = useNames) + x_est1 <- colWeightedMedians(t(x), w = w, useNames = useNames) + stopifnot(all.equal(x_est1, x_est0)) + } + } > > > # Weighted row medians (excluding some columns) > w <- c(1, 1, 0, 1) > x_est0 <- rowMedians(x[, (w == 1), drop = FALSE]) > x_est1 <- rowWeightedMedians(x, w = w) > stopifnot(all.equal(x_est1, x_est0)) > x_est2 <- colWeightedMedians(t(x), w = w) > stopifnot(all.equal(x_est2, x_est0)) > # Test with and without dimnames on x > for (setDimnames in c(TRUE, FALSE)) { + if (setDimnames) dimnames(x) <- dimnames + else dimnames(x) <- NULL + # Check names attribute + for (useNames in c(if (!matrixStats:::isUseNamesNADefunct()) NA, TRUE, FALSE)) { + x_est0 <- rowWeightedMedians_R(x, w = w, useNames = useNames) + x_est1 <- rowWeightedMedians(x, w = w, useNames = useNames) + stopifnot(all.equal(x_est1, x_est0)) + x_est0 <- colWeightedMedians_R(t(x), w = w, useNames = useNames) + x_est1 <- colWeightedMedians(t(x), w = w, useNames = useNames) + stopifnot(all.equal(x_est1, x_est0)) + } + } > > # Weighted row medians (excluding some columns) > w <- c(0, 1, 0, 0) > x_est0 <- rowMedians(x[, (w == 1), drop = FALSE]) > x_est1 <- rowWeightedMedians(x, w = w) > stopifnot(all.equal(x_est1, x_est0)) > x_est2 <- colWeightedMedians(t(x), w = w) > stopifnot(all.equal(x_est2, x_est0)) > # Test with and without dimnames on x > for (setDimnames in c(TRUE, FALSE)) { + if (setDimnames) dimnames(x) <- dimnames + else dimnames(x) <- NULL + # Check names attribute + for (useNames in c(if (!matrixStats:::isUseNamesNADefunct()) NA, TRUE, FALSE)) { + x_est0 <- rowWeightedMedians_R(x, w = w, useNames = useNames) + x_est1 <- rowWeightedMedians(x, w = w, useNames = useNames) + stopifnot(all.equal(x_est1, x_est0)) + x_est0 <- colWeightedMedians_R(t(x), w = w, useNames = useNames) + x_est1 <- colWeightedMedians(t(x), w = w, useNames = useNames) + stopifnot(all.equal(x_est1, x_est0)) + } + } > > # Weighted row medians (all zero weights) > w <- c(0, 0, 0, 0) > x_est0 <- rowMedians(x[, (w == 1), drop = FALSE]) > x_est1 <- rowWeightedMedians(x, w = w) > stopifnot(all.equal(x_est1, x_est0)) > x_est2 <- colWeightedMedians(t(x), w = w) > stopifnot(all.equal(x_est2, x_est0)) > # Test with and without dimnames on x > for (setDimnames in c(TRUE, FALSE)) { + if (setDimnames) dimnames(x) <- dimnames + else dimnames(x) <- NULL + # Check names attribute + for (useNames in c(if (!matrixStats:::isUseNamesNADefunct()) NA, TRUE, FALSE)) { + x_est0 <- rowWeightedMedians_R(x, w = w, useNames = useNames) + x_est1 <- rowWeightedMedians(x, w = w, useNames = useNames) + stopifnot(all.equal(x_est1, x_est0)) + x_est0 <- colWeightedMedians_R(t(x), w = w, useNames = useNames) + x_est1 <- colWeightedMedians(t(x), w = w, useNames = useNames) + stopifnot(all.equal(x_est1, x_est0)) + } + } > > # Weighted medians by rows and columns > w <- 1:4 > x_est1 <- rowWeightedMedians(x, w = w) > x_est2 <- colWeightedMedians(t(x), w = w) > stopifnot(all.equal(x_est2, x_est1)) > # Test with and without dimnames on x > for (setDimnames in c(TRUE, FALSE)) { + if (setDimnames) dimnames(x) <- dimnames + else dimnames(x) <- NULL + # Check names attribute + for (useNames in c(if (!matrixStats:::isUseNamesNADefunct()) NA, TRUE, FALSE)) { + x_est0 <- rowWeightedMedians_R(x, w = w, useNames = useNames) + x_est1 <- rowWeightedMedians(x, w = w, useNames = useNames) + stopifnot(all.equal(x_est1, x_est0)) + x_est0 <- colWeightedMedians_R(t(x), w = w, useNames = useNames) + x_est1 <- colWeightedMedians(t(x), w = w, useNames = useNames) + stopifnot(all.equal(x_est1, x_est0)) + } + } > > > # Weighted row medians with missing values > x_est0 <- apply(x, MARGIN = 1L, FUN = weightedMedian, w = w, na.rm = TRUE) > print(x_est0) [1] -0.04493361 0.23519330 0.73832471 0.65759463 0.59390132 > x_est1 <- rowWeightedMedians(x, w = w, na.rm = TRUE) > print(x_est1) [1] -0.04493361 0.23519330 0.73832471 0.65759463 0.59390132 > stopifnot(all.equal(x_est1, x_est0)) > x_est2 <- colWeightedMedians(t(x), w = w) > stopifnot(all.equal(x_est2, x_est0)) > # Test with and without dimnames on x > for (setDimnames in c(TRUE, FALSE)) { + if (setDimnames) dimnames(x) <- dimnames + else dimnames(x) <- NULL + # Check names attribute + for (useNames in c(if (!matrixStats:::isUseNamesNADefunct()) NA, TRUE, FALSE)) { + x_est0 <- rowWeightedMedians_R(x, w = w, na.rm = TRUE, useNames = useNames) + x_est1 <- rowWeightedMedians(x, w = w, na.rm = TRUE, useNames = useNames) + stopifnot(all.equal(x_est1, x_est0)) + x_est0 <- colWeightedMedians_R(t(x), w = w, na.rm = TRUE, useNames = useNames) + x_est1 <- colWeightedMedians(t(x), w = w, na.rm = TRUE, useNames = useNames) + stopifnot(all.equal(x_est1, x_est0)) + } + } > > # Weighted medians by rows and columns > w <- 1:4 > x_est1 <- rowWeightedMedians(x, w = w, na.rm = TRUE) > x_est2 <- colWeightedMedians(t(x), w = w, na.rm = TRUE) > stopifnot(all.equal(x_est2, x_est1)) > # Test with and without dimnames on x > for (setDimnames in c(TRUE, FALSE)) { + if (setDimnames) dimnames(x) <- dimnames + else dimnames(x) <- NULL + # Check names attribute + for (useNames in c(if (!matrixStats:::isUseNamesNADefunct()) NA, TRUE, FALSE)) { + x_est0 <- rowWeightedMedians_R(x, w = w, na.rm = TRUE, useNames = useNames) + x_est1 <- rowWeightedMedians(x, w = w, na.rm = TRUE, useNames = useNames) + stopifnot(all.equal(x_est1, x_est0)) + x_est0 <- colWeightedMedians_R(t(x), w = w, na.rm = TRUE, useNames = useNames) + x_est1 <- colWeightedMedians(t(x), w = w, na.rm = TRUE, useNames = useNames) + stopifnot(all.equal(x_est1, x_est0)) + } + } > > > # Inf weight > x <- matrix(1:2, nrow = 1, ncol = 2) > w <- c(7, Inf) > x_est1 <- rowWeightedMedians(x, w = w) > x_est2 <- colWeightedMedians(t(x), w = w) > stopifnot(identical(2, x_est1)) > stopifnot(identical(2, x_est2)) > # Test with and without dimnames on x > dimnames <- list("a", LETTERS[1:2]) > for (setDimnames in c(TRUE, FALSE)) { + if (setDimnames) dimnames(x) <- dimnames + else dimnames(x) <- NULL + # Check names attribute + for (useNames in c(if (!matrixStats:::isUseNamesNADefunct()) NA, TRUE, FALSE)) { + x_est0 <- rowWeightedMedians_R(x, w = w, useNames = useNames) + x_est1 <- rowWeightedMedians(x, w = w, useNames = useNames) + stopifnot(all.equal(x_est1, x_est0)) + x_est0 <- colWeightedMedians_R(t(x), w = w, useNames = useNames) + x_est1 <- colWeightedMedians(t(x), w = w, useNames = useNames) + stopifnot(all.equal(x_est1, x_est0)) + } + } > > proc.time() user system elapsed 0.21 0.04 0.26