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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:39:33+00:00 2026-06-14T08:39:33+00:00

I have a simple array indexing question for multi-dimensional arrays in R. I am

  • 0

I have a simple array indexing question for multi-dimensional arrays in R. I am doing a lot of simulations that each give a result as a matrix, where the entries are classified into categories. So for example a result looks like

aresult<-array(sample(1:3, 6, replace=T), dim=c(2,5), 
               dimnames=list(
                 c("prey1", "prey2"), 
                 c("predator1", "predator2", "predator3", "predator4", "predator5")))

Now I want to store the results of my experiments in a 3D-matrix, where the first two dimension are the same as in aresult and the third dimension holds the number of experiments that fell into each category. So my arrays of counts should look like

Counts<-array(0, dim=c(2, 5, 3), 
              dimnames=list(
                c("prey1", "prey2"), 
                c("predator1", "predator2", "predator3", "predator4", "predator5"),
                c("n1", "n2", "n3")))

and after each experiment I want to increment the numbers in the third dimension by 1, using the values in aresults as indexes.

How can I do that without using loops?

  • 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-14T08:39:34+00:00Added an answer on June 14, 2026 at 8:39 am

    This sounds like a typical job for matrix indexing. By subsetting Counts with a three column matrix, each row specifying the indices of an element we want to extract, we are free to extract and increment any elements we like.

    # Create a map of all combinations of indices in the first two dimensions
    i <- expand.grid(prey=1:2, predator=1:5)
    
    # Add the indices of the third dimension
    i <- as.matrix( cbind(i, as.vector(aresult)) )
    
    # Extract and increment
    Counts[i] <- Counts[i] + 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple JSON array that is returned from a zip code passed
I have simple AJAX function that uses jQuery to return an array of 300
I have had this question for a while. Assuming I have a simple array,
I have a simple PHP Array called $categories that looks like this: Array (
I have a simple recursive array function that looks like this: function recursive_array($results) {
I have a simple array that looks like this: array (size=6) 0 => array
I have a simple array that I am trying to handle. It consists of
I have a simple array that I need to serialize as part of a
I would like to have a simple a method, that can give back PHP
I have a simple array search I'm doing with php. $qs = 'something$'; //

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.