20

I have a problem with my numeric vector and dim() in R. I want to know the dimensions of my vector X with:

dim(X)

However, that function returns NULL.

If I type:

X

I can see that the X is not empty. Why does dim or nrow report it as "NULL"?

Part of X:
[93486] 6.343e-01 6.343e-01 6.343e-01 6.343e-01 6.343e-01 6.343e-01 6.346e-01
[93493] 6.346e-01 6.347e-01 6.347e-01 6.347e-01 6.347e-01 6.347e-01 6.347e-01
[93500] 6.347e-01 6.347e-01 6.347e-01 6.347e-01 6.347e-01 6.347e-01 6.347e-01
[93507] 6.347e-01 6.347e-01 6.347e-01 6.347e-01 6.347e-01 6.347e-01 6.347e-01
[93514] 6.347e-01 6.347e-01 6.347e-01 6.347e-01 6.347e-01 6.347e-01 6.347e-01
[93521] 6.347e-01 6.347e-01 6.347e-01 6.348e-01 6.348e-01 6.348e-01 6.348e-01
[93528] 6.348e-01 6.348e-01 6.348e-01 6.348e-01 6.348e-01 6.348e-01 6.348e-01
[93535] 6.348e-01 6.348e-01 6.348e-01 6.348e-01 6.348e-01 6.348e-01 6.348e-01
[93542] 6.348e-01 6.348e-01 6.348e-01 6.348e-01 6.348e-01 6.348e-01 6.348e-01
[93549] 6.348e-01 6.348e-01 6.348e-01 6.348e-01 6.348e-01 6.348e-01 6.348e-01
[93556] 6.348e-01 6.348e-01 6.349e-01 6.349e-01 6.349e-01 6.349e-01 6.349e-01
[93563] 6.349e-01 6.349e-01 6.349e-01 6.349e-01 6.349e-01 6.349e-01 6.349e-01
[93570] 6.349e-01 6.349e-01 6.349e-01 6.349e-01 6.349e-01 6.349e-01 6.349e-01

> dim(X)
NULL
> class(X)
[1] "numeric"
> nrow(pvals_vector)
NULL

Why is there no dimensions of X?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
user1261558
  • 369
  • 1
  • 2
  • 11

2 Answers2

21

Because it is a one-dimensional vector. It has length. Dimensions are extra attributes applied to a vector to turn it into a matrix or a higher dimensional array:

x <- 1:6
dim( x )
#NULL

length( x )
#[1] 6

dim( matrix( x , 2 , 3 ) )
#[1] 2 3
Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
  • Then I have a problem. I want to give names to the rows of my vector like this: > rownames(X) <- pvals$V1 Error in `rownames<-`(`*tmp*`, value = c(35108L, 41354L, 142094L, 5816L, : attempt to set 'rownames' on an object with no dimensions Cannot set rownames due to no dimensions of my object? – user1261558 Aug 28 '13 at 07:50
  • Do it like this: `x <- matrix( x , length(x) , 1 ); rownames(x) <- whatever` that will give you a one column matrix which you can apply rownames to. Or just do for example: `names(x) <- whatever` if you just want a named vector. – Simon O'Hanlon Aug 28 '13 at 07:52
  • 1
    @user1261558 Great! Also since you are relatively new here you might want to read the [**about**](http://stackoverflow.com/about) and the [**faq**](http://stackoverflow.com/faq) about how SO works. StackOverflow is made much more valuable to everyone if when you receive an answer that solves your problem, you accept it by clicking the little check mark or upvote a useful answer. You are under absolutely no obligation to do either, but it is a great way to "give back" to the site if an answer did in fact solve your problem. Thanks! – Simon O'Hanlon Aug 28 '13 at 07:57
2

As a side note, I wrote a function which returns length if dim==NULL :

edit June 2019:

I rewrote this function so it doesn't foul up calls to base::dim inside any existing functions.

# return dim() when it's sensible and length() elsewise
#  let's not allow multiple inputs, just like base::dim, base::length
# Interesting fact --  the function  "dim" and the function  " dim<-" are different
# primitives, so this function here doesn't interfere with the latter.
dim <- function(item) {
        if (is.null(base::dim(item)) ) { 
            dims<-length(item)  
            } else{
                dims  <- base::dim(item)  
                }
    return(dims)
    }

Below is the original posted code

function(items) {
        
        dims<-vector('list',length(items))
        names(dims)<-items
        for(thing in seq(1,length(items))) {
                if (is.null(dim(get(items[thing])))) {

                        dims[[thing]]<-length(get(items[thing]))
                        } else{
                                #load with dim()
                                dims[[thing]]<-dim(get(items[thing]))
                                }
                }
        return(dims)
        }

Or, as SimonO pointed out, you can "force" a 1xN matrix if desired.

Community
  • 1
  • 1
Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73