Distinguishing a vector from a matrix in R

matrix, r, vector

Solution

Seems like you want to ignore any dimension that only has one level, so `drop` would be appropriate:

if(is.null(dim(drop(x)))) {
  # do vector stuff
} else {
  # do matrix/array stuff
}

Problem

I'm making a helper function for this project I'm working on in which I need to make percentiles out of a set of data. In some instances, I'll be percentiling a vector of entries, which is fairly easy. In other instances, I'll be percentiling entries in a matrix. The processes are similar, but different. I'd like to be able to distinguish what is given as an input (whether it's a vector or a matrix) so I know what operation is appropriate. I thought about doing something with the dimensions of the input. But `dim(*vector*) = NULL`, but `dim(matrix(1:15, 1,15)) = c(1,15)` even though that is debatable to be a vector. So I can't use my first idea of ``` if(length(dim(objects)) == 2){*A MATRIX*} else{*A VECTOR*} ``` I considered that I could just add the condition of `min(dim(objects)) > 1` to test for a matrix, but I'm thinking there is probably a better option. (And now I'm here...) Any thoughts?

Original source