Skip to content

Commit

Permalink
Have NCOL(NULL) return 0 instead of 1.
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.r-project.org/R/trunk@85704 00db46b3-68df-0310-9c12-caf00c1e9a41
  • Loading branch information
hornik committed Dec 19, 2023
1 parent 36b76eb commit c786526
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 12 deletions.
3 changes: 3 additions & 0 deletions doc/NEWS.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@
convention once Unicode restricted the number of Unicode points
to \eqn{2{^31} - 1}{2^31 - 1} and so will never need more than 6
digits.)

\item \code{NCOL(NULL)} now returns 0 instead of 1, for
consistency with \code{cbind()}.
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/library/base/R/matrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ nrow <- function(x) dim(x)[1L]
ncol <- function(x) dim(x)[2L]

NROW <- function(x) if(length(d <- dim(x))) d[1L] else length(x)
NCOL <- function(x) if(length(d <- dim(x)) > 1L) d[2L] else 1L
NCOL <- function(x)
if(is.null(x)) 0L else if(length(d <- dim(x)) > 1L) d[2L] else 1L

rownames <- function(x, do.NULL = TRUE, prefix = "row")
{
Expand Down
8 changes: 5 additions & 3 deletions src/library/base/man/nrow.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ NROW(1:12) # 12, the length() of the vector
## and so does cbind() :
dim(as.matrix(numeric())) # 0 1
dim( cbind(numeric())) # ditto
## consequently, NCOL(.) gives 1, too :
NCOL(numeric()) # 1 and hence
NCOL(NULL) # 1
NCOL(numeric()) # 1
## However, as.matrix(NULL) fails and cbind(NULL) gives NULL, hence for
## consistency:
NCOL(NULL) # 0
## (This gave 1 in R < 4.4.0.)
}
\keyword{array}
8 changes: 0 additions & 8 deletions src/library/profile/Common.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,3 @@ local({

makeActiveBinding(".Library.site", slfun, globalenv())
})

if(isTRUE(as.logical(Sys.getenv("_R_NCOL_NULL_IS_ZERO_",
"FALSE")))) {
NCOL <- function(x)
if(is.null(x)) 0L
else if(length(d <- dim(x)) > 1L) d[2L]
else 1L
}

0 comments on commit c786526

Please sign in to comment.