title: R: Keeping numbers of loops flexible (by addressing dimensions without indexing)
body: <p>Hello little and big helpers,</p>
  
  <p>I have a problem in R concerning a dataset with variable number of dimensons, causing a changing number of for-loops. I will provide an example that is somewhat similar to my problem (albeit of course different topic).</p>
  
  <p>Imagine we have the following data set: in our R-wonderland there are 5 states. In each state there are 6 towns. In each town there are 10 burger restaurants. We know the burger price for each of the 10*6*5 = 300 restaurants. Now I'd like to know the cheapest burger for every town in every state.</p>
  
  <pre><code>x &lt;- rnorm(300,5,1) # create random "burger prices" with mean=5$ and std.dv 1$ for all 300 restaurants
  x_Array &lt;- array(x,dim=c(10,6,5)) # get the prices in order: 1st dim = 10 restaurants per town, 2nd dim = 6 towns in state, 3rd dim = 5 states
  
  x_Subset &lt;- array(0,dim=c(dim(x_Array)[2],dim(x_Array)[3])) # prepare a subset which will contain "location" of the cheapest burger in town &amp; state
  
  # find cheapest burger for each town in each state -&gt; this I want to keep flexible in terms of number of dimensions/loops
  for (i in 1:dim(x_Array)[2]){
    for (j in 1:dim(x_Array)[3]){
      x_Subset[i,j] &lt;- which(x_Array == min(x_Array[,i,j]))
    }
  }
  </code></pre>
  
  <p>By eliminating the variation of restaurants we reduce the original data set by one dimension (10 restaurants -> cheapest ONE restaurant). </p>
  
  <p>The problem is that in my case the dimesions are not fixed. Imagine I'd like to add different nations to my data set, so there might be 10 restaurants in 6 towns and 5 states of 4 nations on 3 continents - this 5D problem would result in an x_Subset containing 4 dimensions. I'd have to set up a loop of depth four.</p>
  
  <p>How do I keep those numbers of loops flexible? I had a similar problem which I was able to solve using </p>
  
  <pre><code>Expand.grid()
  </code></pre>
  
  <p>But that does not reduce the dimension by one. There would be a new item for restaurant 1,2,3,... for each town, state (,nation, continent, ...). However I only need the cheapest restaurant (> min()) on the first dim and all combinations for the rest of the dataset.</p>
  
  <p>I think it would be possible if I could only solve this synthax issue:</p>
  
  <pre><code>x_Subset_vector &lt;- x_Subset[,1] # how do I write this without knowing the n° of dimensions of x_Subset? Like: "get all values of the first item (in this case: "column") in the second dimension"
  </code></pre>
  
  <p>Or for the burger example:</p>
  
  <pre><code>x_Subset_vector &lt;- x_Subset[,1,1] # 3D problem ([restaurants, towns, states])
  x_Subset_vector &lt;- x_Subset[,1,1,1,1] # 5D problem ([..., nation,continent])
  </code></pre>
  
  <p>In easy words: how do I automize the numbers of "commas" in the index of x_Subset?</p>
  
  <p>Sorry for making you hungry. You deserve hell of a great burger for helping me with this one!</p>
  
  <p>Thank you so much, guys!</p>