R Inconsistencies

A collection of detected R inconsistencies and bugs.

0 dimension matrix

Subsetting a matrix by selecting 0 rows and 0 columns preserves matrix format even without drop=FALSE.

matrix()[1,0]                          matrix()[0,0]
## integer(0)                          ## <0 x 0 matrix>

qnorm() on empty matrix

qnorm() drops matrix format when input is an empty matrix.

qnorm(matrix(0.1, nrow=1, ncol=2))     qnorm(matrix(0.1, nrow=0, ncol=0))
##           [,1]      [,2]            ## numeric(0)
## [1,] -1.281552 -1.281552

is.nan() on data.frame

is.nan doesn’t work on data.frames while is.na does.

is.na(iris)                            is.nan(iris)
## <works>                             ## <error>

rbind() on zero-column data.frames

rbind() does not preserve the number of rows for data.frames, while cbind() does.

mat <- matrix(nrow=2, ncol=0)
df  <- as.data.frame(mat)


dim(mat)                               dim(df)
## [1] 2 0                             ## [1] 2 0

dim(cbind(mat, mat))                   dim(cbind(df, df))
## [1] 2 0                             ## [1] 2 0

dim(rbind(mat, mat))                   dim(rbind(df, df))
## [1] 4 0                             ## [1] 0 0

Number of columns are always preserved, making rbind and number of rows the only exception.

any() doesn’t work on data.frames

any() works on numeric data.frames but not on logical data.frames.

any(data.frame(A=0, B=1))              any(data.frame(A=TRUE, B=FALSE))
## <warning>                           ## <error>
## TRUE

rbind() on nested data.frames

rbind() on data.frame do not adjust row names to be unique when the data.frame is nested.

df    <- data.frame(a=1:3)
df$df <- data.frame(a=1:3)


rbind(df, df)
## <error>

flinger.test() and constant values

fligner.test() can return significant p-value for constant variance.

fligner.test(c(1,1,2,2), c(1,1,2,2))   fligner.test(c(1,1,2,2,2), c(1,1,2,2,2))
## <pvalue = NA>                       ## <pvalue < 2.2e-16>

paired wilcoxon.test() and ties

Paired versions of wilcoxon.test() has tolerance issues when detecting if ties are present.

wilcox.test(c(4,3,2), c(3,2,1), paired=TRUE)
## <warning about ties>


wilcox.test(c(0.4,0.3,0.2), c(0.3,0.2,0.1), paired=TRUE)
## <no warning>

paired wilcoxon.test() and Inf

All versions of wilcoxon.test() remove infinite values before proceeding, except when paired=TRUE. With non-paired version Inf values are removed and results below are different.

wilcox.test(c(1,2,3,4), c(0,9,8,7))
## <pvalue = 0.3429>


wilcox.test(c(1,2,3,4), c(0,9,8,Inf))
## <pvalue = 0.6286>

Paired version leaves Inf and includes it in the ranks, making the results below equivalent.

wilcox.test(c(1,2,3,4), c(0,9,8,7), paired=TRUE)
## <p-value = 0.25>


wilcox.test(c(1,2,3,4), c(0,9,8,Inf), paired=TRUE)
## <p-value = 0.25>

paired wilcoxon.test() and warning

Paired wilcox.test() throws an error about x when y observations are missing.

wilcox.test(c(1,2), c(NA_integer_,NA_integer_), paired=TRUE)
## <error about not enough 'x' observations>

wilcoxon.test() output details

wilcox.test() result does not indicate if exact test was used or not.

wilcox.test(rnorm(10), exact=FALSE, correct=FALSE)
## <name = Wilcoxon signed rank test>


wilcox.test(rnorm(10), exact=TRUE, correct=FALSE)
## <name = Wilcoxon signed rank test>

var.test() and conf.level

var.test does not accept conf.level of either 0 or 1, while t.test does.

t.test(rnorm(10), rnorm(10), conf.level=0)
## <works>


var.test(rnorm(10), rnorm(10), conf.level=0)
## <error>

  1. ◦  Worth noting that this behaviour is documented in the manual pages of ?rbind.data.frame.