Supposed that X
contains 1000 rows with m
columns, where m
equal to 3 as follows:
set.seed(5)
X <- cbind(rnorm(1000
You can use NROW
for both cases. From ?NROW
nrow
andncol
return the number of rows or columns present inx
.NCOL
andNROW
do the same treating a vector as 1-column matrix.
So that means that even if the subset is dropped down to a vector, as long as x
is an array, vector, or data frame NROW
will treat it as a one-column matrix.
sub1 <- X[,2:3]
is.matrix(sub1)
# [1] TRUE
NROW(sub1)
# [1] 1000
sub2 <- X[,1]
is.matrix(sub2)
# [1] FALSE
NROW(sub2)
# [1] 1000
So if(NROW(X) < 1000L) a + b
should work regardless of whether X
is a matrix or a vector. I use <=
below, since X
has exactly 1000 rows in your example.
a <- 5; b <- 15
if(NROW(sub1) <= 1000L) a + b
# [1] 20
if(NROW(sub2) <= 1000L) a + b
# [1] 20
A second option would be to use drop=FALSE
when you make the variable selection. This will make the subset remain a matrix when the subset is only one column. This way you can use nrow
with no worry. An example of this is
X[, 1, drop = FALSE]