red_tank142
red_tank142

Reputation: 61

Check for vector and matrix data type at once

i was wondering if there is as faster way to check if given data is a vector or a matrix with zero rows or not, rather than,

if (is.vector(x)){
    print('vector')
} else if {nrow(x) == 0){
    print('0 rows')
} else {
    print('matrix').
}

I am only interested in finding matrices fast.

Thanks a lot.

Upvotes: 0

Views: 178

Answers (1)

Karsten W.
Karsten W.

Reputation: 18420

One method would be to use dim:

!is.null(dim(x)) && all(dim(x)>0)

If x is a vector, dim(x) is NULL.

I am not sure if this is faster than your own method and also not sure if this is a computation-intensive task at all.

Upvotes: 1

Related Questions