-1

I have an excel that contains a matrix. Here you find a screenshot of the matrix I want to use: https://www.flickr.com/photos/113328996@N07/23026818939/in/dateposted-public/

What I would like to do now is to create some kind of lookup function. So when i have the rows:

Arsenal - Aston Villa

It should look up 114.6.

Of course I could create rows with all distances like:

Arsenal - Aston Villa - 144.6

And perform a lookup function but my instincts tell me this is not the most efficient way.

Any feedback on how I can deal with above most efficiently?

  • 1
    Don't post images of data. Consider reading [this](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example).My approach would be to melt your data to long format, and then just select relevant rows.\ – Heroka Nov 29 '15 at 12:58

1 Answers1

2

This lookup-function is the basic [ operator for data.frames and matrices in R.

Take this example data (from Here)

a <- cbind(c(0.1,0.5,0.25),c(0.2,0.3,0.65),c(0.7,0.2,0.1))
rownames(a) <- c("Lilo","Chops","Henmans")
colnames(a) <- c("Product A","Product B","Product C")

a
        Product A Product B Product C
Lilo         0.10      0.20       0.7
Chops        0.50      0.30       0.2
Henmans      0.25      0.65       0.1

The lookupfunktion is this:

a["Lilo","Product A"] # 0.1
a["Henmans","Product B"] # 0.65
Community
  • 1
  • 1
Rentrop
  • 20,979
  • 10
  • 72
  • 100
  • @MarcvanderPeet if the answer solves your issue, can you accept it? Keeps others from spending time on it. – Heroka Nov 29 '15 at 15:11