Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8980237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:01:59+00:00 2026-06-15T20:01:59+00:00

Suppose I have a function that takes an argument x of dimension 1 or

  • 0

Suppose I have a function that takes an argument x of dimension 1 or 2. I’d like to do something like

x[1, i]

regardless of whether I got a vector or a matrix (or a table of one variable, or two).

For example:

x = 1:5
x[1,2] # this won't work...

Of course I can check to see which class was given as an argument, or force the argument to be a matrix, but I’d rather not do that. In Matlab, for example, vectors are matrices with all but one dimension of size 1 (and can be treated as either row or column, etc.). This makes code nice and regular.

Also, does anyone have an idea why in R vectors (or in general one dimensional objects) aren’t special cases of matrices (or multidimensional objects)?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:02:01+00:00Added an answer on June 15, 2026 at 8:02 pm

    In R, it is the other way round; matrices are vectors. The matrix-like behaviour comes from some extra attributes on top of the atomic vector part of the object.

    To get the behaviour you want, you’d need to make the vector be a matrix, by setting dimensions on the vector using dim() or explicit coercion.

    > vm <- 1:5
    > dim(vm) <- c(1,5)
    > vm
         [,1] [,2] [,3] [,4] [,5]
    [1,]    1    2    3    4    5
    > class(vm)
    [1] "matrix"
    

    Next you’ll need to maintain the dimensions when subsetting; by default R will drop empty dimensions, which in the case of vm above is the row dimension. You do that using drop = FALSE in the call to '['(). The behaviour by default is drop = TRUE:

    > vm[, 2:4]
    [1] 2 3 4
    > vm[, 2:4, drop = FALSE]
         [,1] [,2] [,3]
    [1,]    2    3    4
    

    You could add a class to your matrices and write methods for [ for that class where the argument drop is set to FALSE by default

    class(vm) <- c("foo", class(vm))
    `[.foo` <- function(x, i, j, ..., drop = FALSE) {
      clx <- class(x)
      class(x) <- clx[clx != "foo"]
      x[i, j, ..., drop = drop]
    }
    

    which in use gives:

    > vm[, 2:4]
         [,1] [,2] [,3]
    [1,]    2    3    4
    

    i.e. maintains the empty dimension.

    Making this fool-proof and pervasive will require a lot more effort but the above will get you started.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a function that I do not control that looks something like
Suppose I have a function f that takes a vector v and returns a
Suppose I have a function that takes an r-value argument and I want to
Suppose I have a function that creates data frames. I'd like to run that
Suppose I have a function/method F() that takes 3 parameters $A, $B and $C
Suppose I have a function that takes two arguments, void f(int x, int y);
Suppose I have a class with a constructor (or other function) that takes a
Suppose I have a function which takes a nullary functor as an argument: void
Suppose I have class Function , whose instances are callables that take one argument.
Suppose I have a function that returns a closure: sub generator { my $resource

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.