Created
April 12, 2016 16:23
-
-
Save ankur-gupta/8702b19ade358a4322acb6d4b7bb5578 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ----------------------------------------------------------------------------------- | |
# Demo of a function that depends upon matrix rownames | |
# ----------------------------------------------------------------------------------- | |
rm(list = ls()) | |
graphics.off() | |
library(geoR) # Tested with geoR_1.7-5.1 | |
# Extract a small matrix for demonstration | |
x <- as.matrix(cars[1:3, ]) | |
print(x) | |
# Duplicate rows of coords with duplicate rownames | |
coords.dup.rownames <- rbind(x, x) | |
# Duplicate rows of coords with unique rownames | |
coords.uni.rownames <- rbind(x, x) | |
rownames(coords.uni.rownames) <- seq_len(nrow(coords.uni.rownames)) | |
# Run the function geoR::jitterDupCoords on both with the same seed | |
set.seed(64651) | |
coords.dup.rownames.jit <- jitterDupCoords(coords.dup.rownames, 1) | |
set.seed(64651) | |
coords.uni.rownames.jit <- jitterDupCoords(coords.uni.rownames, 1) | |
# Look at both the results | |
# Unique rownames in input - expected results | |
print(coords.uni.rownames.jit) | |
print(unique(coords.uni.rownames.jit)) # Jitter has been added to all duplicated coords | |
print(nrow(unique(coords.uni.rownames.jit)) == nrow(coords.uni.rownames.jit)) | |
# Duplicated rownames - unexpected results | |
print(coords.dup.rownames.jit) | |
print(unique(coords.dup.rownames.jit)) # Jitter has NOT been added to all duplicated coords | |
print(nrow(unique(coords.dup.rownames.jit)) == nrow(coords.dup.rownames.jit)) | |
# jitterDupCoords() expects that the rownames be unique for correct working | |
# ------------------------------------------------------------------------------------ | |
# One level deeper: | |
# jitterDupCoords() uses geoR::dup.coords() under the hood (See geoR/R/geoRmisc.R) | |
# dup.coords() identifies which rows are duplicated and stores the rownames not rowindices. | |
# See ?dup.coords() | |
# ------------------------------------------------------------------------------------ | |
# In this case, rownames equals rowindices. | |
# We can see that row named "2" is the same as row named "5", | |
# Similarly, row named "1" is the same as row named "4". | |
print(dup.coords(coords.uni.rownames, simplify=FALSE, USE.NAMES=FALSE)) | |
# We get the "correct" answer from dup.coords(). | |
# Row index 2 is the same as row index 5. But both rows have the same row name of "2". | |
print(dup.coords(coords.dup.rownames, simplify=FALSE, USE.NAMES=FALSE)) | |
# The next step in jitterDupCoords.default() (line 19, geoR/R/geoRmisc.R) uses row names | |
# to fill in the matrix. | |
# x[ind[[i]],] <- jitter2d(coords=x[ind[[i]],], ...) | |
# This causes the unexpected results when the rownames are non-unique. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment