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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:02:41+00:00 2026-05-25T00:02:41+00:00

Suppose I have a simple multidimensional structure, like this one: somestr<-array(sample.int(2, 120, replace=TRUE), dim=c(4,5,6))

  • 0

Suppose I have a simple multidimensional structure, like this one:

somestr<-array(sample.int(2, 120, replace=TRUE), dim=c(4,5,6))

I’m looking for all positions in the structure (in this case, an array) where the value is equal to, say for my example, 2. Note that the structure might just as well hold characters or logicals. For now, it will do to just find all values equal to a given, but it would be nice to extend the idea to any logical-valued function that can be applied to each item in the structure (That would allow e.g. is.nato be used).

What I would like to get, is an (integer) matrix with as many columns as somestr has dimensions (in this case 3), and as many rows (depends on the sample.int call above) as there are values equal to the given value (2). The values in this new matrix are the ‘coordinates’ within somestr where the values are equal to 2.

I apologize for mixing my example with the explanation, but I was hoping it would be clearer that way. For the record: I’m able to produce this myself (may even answer my own question), but I was hoping for a standardized solution (read: a readymade function in some package), or learn new tricks along the way.

So, in short, can you write a function

posOf<-function(somestr, valueToCompareTo)

that returns a matrix of the positions in somestr equal to valueToCompareTo, and if valueToCompareTo is a function, the positions in somestr for which applying this function returns TRUE.

  • 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-05-25T00:02:41+00:00Added an answer on May 25, 2026 at 12:02 am

    I think the which function can do that:

    which(somestr==2, arr.ind=TRUE)
    

    (if I understood everything correctly)

    R> set.seed(123)
    R> somestr <- array(sample.int(2, 120, replace=TRUE), dim=c(4,5,6))
    R> somestr
    , , 1
    
         [,1] [,2] [,3] [,4] [,5]
    [1,]    1    2    2    2    1
    [2,]    2    1    1    2    1
    [3,]    1    2    2    1    1
    [4,]    2    2    1    2    2
    
    ...
    
    , , 6
    
         [,1] [,2] [,3] [,4] [,5]
    [1,]    2    1    1    1    2
    [2,]    1    2    1    2    2
    [3,]    1    2    2    2    2
    [4,]    2    2    1    1    1
    
    R> which(somestr==2, arr.ind=TRUE)
          dim1 dim2 dim3
     [1,]    2    1    1
     [2,]    4    1    1
     [3,]    1    2    1
     [4,]    3    2    1
     [5,]    4    2    1
    ...
    [57,]    2    5    6
    [58,]    3    5    6
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose you have a simple class like this: class foo{ private: int* mData; int
Suppose I have simple table like this: ID (PRIMARY) time (INT) stage (TINYINT) other
Suppose I have some simple struct like this: public struct WeightedInt { public int
Suppose I have a simple table, like this: Smith | Select Jones | Select
Suppose I have a simple Python list like this: >>> l=['0', '1', '2', '3',
Suppose we have a simple query like this: SELECT x FROM t WHERE t.y
Suppose I have a simple MySQL table that looks like this: CREATE TABLE `my_table`
Suppose I have a simple Java class like this: public class User { String
Suppose I have a simple method like this for processing two lists: public static
Hi Suppose I have a simple model class like this: class TestModel(models.Model): testkey =

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.