0

I'm pretty new to R, is there a way to have a two-dimensional list of two-tuples as indexes in R programming language?

For example :

my_list[(1,2)] 
#[1] 7, 10, 3
Sotos
  • 51,121
  • 6
  • 32
  • 66
Winston
  • 601
  • 1
  • 9
  • 29
  • 2
    Could you provide further context and examples? – s_baldur Mar 02 '18 at 10:10
  • 1
    Very unclear. Please provide [reproducible example](http://stackoverflow.com/questions/5963269) and expected output. – Sotos Mar 02 '18 at 10:13
  • @snoram Actually, I'm trying to store adjacent vertices[k] for any two arbitrary vertices [v,u] such that k is connected to both v and u so I need a list that is indexed by two integers as vertex id's and a list for each tuple which is their common adjacent vertices. – Winston Mar 02 '18 at 10:15

1 Answers1

1

Sorry, I cannot comment yet but it kind of sounds like you just mean a matrix.

Please write what exactly you want to achieve. In case you actually mean matrices it work like this:

You can define a matrix, e.g. a 3x3 matrix of numbers 1:9:

A = matrix(1:9, nrow=3, ncol=3)

Then reference the i-th row and j-th column by A[i, j].

A[1, 3]
[1] 7

You can have any number of dimensions using array.

Hope that helps

yassem
  • 41
  • 6
  • Thanks, I solved my problem using a different way but the difference between a matrix and the data structure that I described is that you can have a single value for each tuple in a matrix, but I needed a list of values for each tuple. – Winston Mar 02 '18 at 21:15
  • you mean a list object?(easy difference, one where you write the subscript with double brackets, `list[[i]]'). If you need a 2-touple to correspond to a vector, it sounds like a perfect job for a three-dimensional array. Edit: I just read your other comment, so I suppose your lists would be of different length, and then indeed you might prefer a different solution. – yassem Mar 02 '18 at 22:00
  • Yeah, they're different in length. – Winston Mar 02 '18 at 22:27