Skip to contents
loading...

nr() and nc() are synonyms of the ugly NROW() or NCOL() that get the number of row and columns in a matrix or data frame, but also in a vector (they return a value even if the dim attribute of the object is not set, on the contrary to nrow()or ncol()).

ROWS and COLS are constants that makes call to apply() more expressive. ROWS = 1L and COLS = 2L.

Usage

nr(x)

nc(x)

ROWS

COLS

Format

An object of class integer of length 1.

An object of class integer of length 1.

Arguments

x

Any object.

See also

Examples

mm <- matrix(1:6, nrow = 3)
nr(mm)
#> [1] 3
nc(mm)
#> [1] 2

vv <- 1:6
nr(vv)
#> [1] 6
nc(vv)
#> [1] 1

# ROWS and COLS constants used with apply()
apply(mm, ROWS, mean) # Idem apply(mm, 1, mean)
#> [1] 2.5 3.5 4.5
apply(mm, COLS, mean) # Idem apply(mm, 2, mean)
#> [1] 2 5