What Is Diffrence Between Nrow and Nrow in R? [Duplicate]

if we already have nrow and ncol in r, then why there is NROW and NCOL is there. is there any difference between them or they are just an alias?

mx <- matrix(1:12,3,4)

nrow(mx)
NROW(mx)

ncol(mx)
NCOL(mx)
1

in R you always can check the code of functions, typing their names without the parenthesis. Doing this, you can see the differences between NCOL and ncol:

NCOL
  # function (x) 
  # if (length(d <- dim(x)) > 1L) d[2L] else 1L
  # <bytecode: 0x560bca6cb290>
  # <environment: namespace:base>

ncol
  # function (x) 
  # dim(x)[2L]
  # <bytecode: 0x560bc9691cd0>
  # <environment: namespace:base>

ncol will always return the second dimension of the argument, while NCOL will check if the argument only has one dimension, in which case it will return 1.

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest