nrow {base} | R Documentation |
The Number of Rows/Columns of an Array
Description
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, even a 0-length vector, compatibly with
as.matrix()
or cbind()
, see the example.
Usage
nrow(x)
ncol(x)
NCOL(x)
NROW(x)
Arguments
x |
a vector, array, data frame, or |
Value
an integer
of length 1 or NULL
, the
latter only for ncol
and nrow
.
References
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
The New S Language.
Wadsworth & Brooks/Cole (ncol
and nrow
.)
See Also
dim
which returns all dimensions, and
length
which gives a number (a ‘count’) also in cases where
dim()
is NULL
, and hence nrow()
and ncol()
return NULL
;
array
, matrix
.
Examples
ma <- matrix(1:12, 3, 4)
nrow(ma) # 3
ncol(ma) # 4
ncol(array(1:24, dim = 2:4)) # 3, the second dimension
NCOL(1:12) # 1
NROW(1:12) # 12, the length() of the vector
## as.matrix() produces 1-column matrices from 0-length vectors,
## and so does cbind() :
dim(as.matrix(numeric())) # 0 1
dim( cbind(numeric())) # ditto
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.)
[Package base version 4.4.0 Index]