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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:34:27+00:00 2026-06-15T03:34:27+00:00

I need a function that subsets a multidimensional array in R; the catch is

  • 0

I need a function that subsets a multidimensional array in R; the catch is I don’t know which dimension or the length along that dimension until the function is called.

# subset a 3-d array; leave dims 1 and 2, but start 3rd dim at its 11th value
mydim <- dim(myarr)
myarr[, , 11:mydim[3]]

# subset a 4-d array; leave dims 1, 3 and 4, but start 2rd dim at its 8th value
mydim <- dim(myarr)
myarr[, 8:mydim[2], , ]

I always need to subset along exactly one dimension, and the subsetting is always to start at some value other than 1. I also need to preserve the array structure, so matrix indexing into arrays does not look appealing. Thanks in advance.

  • 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-15T03:34:28+00:00Added an answer on June 15, 2026 at 3:34 am

    Here’s an option that takes advantage of the possibility of subsetting an array based on a matrix:

    myarr <- array(1:(2*3*4), dim = c(2, 3, 4))
    
    myfun <- function(arr, from, len, Dim){
        dimArr <- dim(arr)
        if(missing(len)){
            subIdx <- from:dimArr[Dim]
        } else {
            subIdx <- from:(from + len - 1)
        }
        arrD <- lapply(as.list(dimArr), seq_len)
        arrD[[Dim]] <- subIdx
        subMat <- as.matrix(do.call(expand.grid, arrD))
        array(arr[subMat], dim = lapply(arrD, length))
    }
    
    > myfun(myarr, 2, 1, 3)
    , , 1
    
         [,1] [,2] [,3]
    [1,]    7    9   11
    [2,]    8   10   12
    
    > myfun(myarr, 2, Dim = 3)
    , , 1
    
         [,1] [,2] [,3]
    [1,]    7    9   11
    [2,]    8   10   12
    
    , , 2
    
         [,1] [,2] [,3]
    [1,]   13   15   17
    [2,]   14   16   18
    
    , , 3
    
         [,1] [,2] [,3]
    [1,]   19   21   23
    [2,]   20   22   24
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that can intersect 2 arrays for example: $Array1 = array(1,2,3);
I need a function that randomizes an array similar to what shuffle does, with
I need a function that could generate a regex. For example if I write
I need a function that can print Turkish characters. public String convert(String input) {
I need a function that can return the difference between the below two dates
I need a function that returns the ASCII value of a character, including spaces,
I need a function that a user can just click a flash app button
I need a function that will convert a type to a string, for example:
I need a function that returns the substring between two words (or two characters).
I need a function that will take a bitmap and return the bitmap with

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.