How to check whether inputs are of same type and size in R?
<p>I want to write a function that accepts only vectors of same size, and only matrices of same size, how can I achieve this? </p>

<p>I have a function like shown below, but it clearly won't work like that, because if I have valid vectors, I will get an error that they are not valid matrices, and if they are valid matrices vice versa. But, as I said I only want to check whether they are vector or matrix, and whether they are of same size, only if they are of same type and size I want to continue with the function.</p>

<pre><code>testFunc &lt;- function(x, y)
{
  if (!(is.vector(x) &amp;&amp; is.vector(y) &amp;&amp; is.numeric(x) &amp;&amp; is.numeric(y) &amp;&amp; length(x) == length(y)))
    stop("Invalid input.")
  if (!(is.matrix(x) &amp;&amp; is.matrix(y) &amp;&amp; is.numeric(x) &amp;&amp; is.numeric(y) &amp;&amp; dim(x) == dim(y)))
    stop("Invalid input.")

   # Rest of the function
}
</code></pre>

<p>How can I make these checks, and in most concise way?</p>