Services & Resources / Wolfram Forums / MathGroup Archive

MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

vlookup like function sugestion

  • To: mathgroup at
  • Subject: [mg125778] vlookup like function sugestion
  • From: Rodrigo Murta <murta at>
  • Date: Mon, 2 Apr 2012 04:23:30 -0400 (EDT)
  • Delivered-to:

When you are used to work with Excel, one big challenge in start
working with Mathematica is to find one way to merge table like
Vlookup does.
I know by my own experience that it's very difficult to handle this in
Mathematica, and I belive that a lot of Excel user get a little bit
frustrated in see that one simple operation like that is so difficult.
I would like to suggest to Mathematica team that the future version
could have some builting in function to handle this situation.
Meanwhile I use the below function that I would like to share with the

vlookup[data1_, data2_, pk1_: 1, col1_: {2}, pk2_: 1] :=
  look[val_, table_, pk2l_] := Select[table, #[[pk2l]] == val &];
     Check[look[#[[pk1]], data2, pk2][[1, col1]],
       ConstantArray[Null, Length@col1]] // Quiet] & /@ data1]

Use example:


vlookup[mydata, lookup, 3, {2, 3}]

{{"brazil", 605, 54161, a, 3.2, "rodrigo"}, {"india", 550.92, 49505,
  b, 12.3, "daniel"}, {"china", 550.92, 49505, c, 12.3,
  "daniel"}, {"russia", 752.61, 54324, d, Null, Null}}

vlookup[mydata, lookup, 3]

{{"brazil", 605, 54161, a, 3.2}, {"india", 550.92, 49505, b,
  12.3}, {"china", 550.92, 49505, c, 12.3}, {"russia", 752.61, 54324,
  d, Null}}

Best Regards
PS: there is an topic about this in 2008

  • Prev by Date: Re: Dynamic list creation
  • Next by Date: Re: Integers that are the sum of 2 nonzero squares in exactly 2 ways
  • Previous by thread: Re: How do I stop Mathematica changing the format of fractions in Input cells?
  • Next by thread: Re: Integers that are the sum of 2 nonzero squares in exactly 2 ways