1

As pointed out in this question and copious official & unofficial R documentation,

x <- complex(15)
dim(x) == NULL

For me it's annoying to have to write a separate method (or if clause) for atomic vectors rather than being able to use dim(x)[1]. Would it be stupid to recode dim (a primitive) so it automatically returns length if dim(x)==NULL?

To be a bit more concrete: Are popular packages going to break if I recode dim in let's say my .Rprofile? Is this stupid for another reason I'm not seeing?

Community
  • 1
  • 1
isomorphismes
  • 8,233
  • 9
  • 59
  • 70

1 Answers1

4

It's unclear what you're trying to do, but the NROW and NCOL functions are ways to retrieve extents in a dimension-agnostic way. They treat vectors as column vectors, so NROW(X) is the same as length(x) and NCOL(x) is 1 when x is a vector.

> x <- numeric(10) # or complex, character, logical, etc
> nrow(x)
NULL
> NROW(x)
[1] 10
> NCOL(x)
[1] 1

> m <- matrix(1:10, nrow=5)
> nrow(m)
[1] 5
> NROW(m)
[1] 5
> NCOL(m)
[1] 2
Hong Ooi
  • 56,353
  • 13
  • 134
  • 187