Supposed that X
contains 1000 rows with m
columns, where m
equal to 3 as follows:
set.seed(5)
X <- cbind(rnorm(1000,0,0.5), rnorm(1000,0,0.5), rnorm(1000,0,0.5))
Variable selection is performed, then the condition will be checked before performing the next operation as follows.
if(nrow(X) < 1000){print(a+b)}
,where a
is 5 and b
is 15, so if nrow(X) < 1000
is TRUE
, then 20 will be printed out.
However, in case that X
happens to be a vector because only one column is selected,
how can I check the number of data points when X can be either a matrix or vector ?
What I can think of is that
if(is.matrix(X)){
n <- nrow(X)
} else {
n <- length(X)}
if(n < 1000){print(a+b)}
Anyone has a better idea ?
Thank you
You can use NROW
for both cases. From ?NROW
nrow
and ncol
return the number of rows or columns present in x
. NCOL
and NROW
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]