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 9222207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:46:41+00:00 2026-06-18T03:46:41+00:00

I have an array in R, created by a function like this: A <-

  • 0

I have an array in R, created by a function like this:

A <- array(data=NA, dim=c(2,4,4), dimnames=list(c("x","y"),NULL,NULL))

And I would like to select along one dimension, so for the example above I would have:

A["x",,]
dim(A["x",,])    #[1] 4 4

Is there a way to generalize if I do not know in advance how many dimensions (in addition to the named one I want to select by) my array might have? I would like to write a function that takes input that might formatted as A above, or as:

B <- c(1,2)
names(B) <- c("x", "y")

C <- matrix(1, 2, 2, dimnames=list(c("x","y"),NULL))

Background

The general background is that I am working on an ODE model, so for deSolve’s ODE function it must take a single named vector with my current state. For some other functions, like calculating phase-planes/direction fields, it would be more practical to have a higher-dimensional array to apply the differential equation to, and I would like to avoid having many copies of the same function, simply with different numbers of commas after the dimension I want to select.

  • 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-18T03:46:42+00:00Added an answer on June 18, 2026 at 3:46 am

    I spent quite a lot of time figuring out the fastest way to do this for plyr, and the best I could come up with was manually constructing the call to [:

    index_array <- function(x, dim, value, drop = FALSE) { 
      # Create list representing arguments supplied to [
      # bquote() creates an object corresponding to a missing argument
      indices <- rep(list(bquote()), length(dim(x)))
      indices[[dim]] <- value
    
      # Generate the call to [
      call <- as.call(c(
        list(as.name("["), quote(x)),
        indices,
        list(drop = drop)))
      # Print it, just to make it easier to see what's going on
      print(call)
    
      # Finally, evaluate it
      eval(call)
    }
    

    (You can find more information about this technique at https://github.com/hadley/devtools/wiki/Computing-on-the-language)

    You can then use it as follows:

    A <- array(data=NA, dim=c(2,4,4), dimnames=list(c("x","y"),NULL,NULL))
    index_array(A, 2, 2)
    index_array(A, 2, 2, drop = TRUE)
    index_array(A, 3, 2, drop = TRUE)
    

    It would also generalise in a straightforward way if you want to extract based on more than one dimension, but you’d need to rethink the arguments to the function.

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

Sidebar

Related Questions

I have a cell array of anonymous function handles, and would like to create
I have created a list box like this, and created a title bar which
I have created a JS array like this var detailsArr = new Array(); and
I have a knockout.js ViewModel with an observable array: function ItemsViewModel() { this.data =
I have code like this: function mgl_get_by($col,$id) { $this->db->select('*'); $this->db->from('global_info'); $this->db->join('ad', 'id_ad = id_global_info');
Let's say you have a JavaScript class like this var DepartmentFactory = function(data) {
I have an array created with this code: var widthRange = new Array(); widthRange[46]
I have created an array in php that prints this Array ( [mark] =>
I have an array in Knockout view model which looks like so: this.Activities =
I have a multidimensional array with various config settings. Here's an example: $this->data =

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.