# Extracting rows from a table / dataset based on arbitrary ordering function

Please look at the following clip of data. Suppose it is from a huge data set containing 100 variables x 10,000 names:

Name    x1  x2  x3  x4  x5
Abe -0.539010   0.75445 -1.44697    -0.06767    1.69325
Bob -0.992619   -2.20993    1.73367 2.01081 -0.94835
Carl    -0.821359   1.08176 0.24655 0.66602 0.20304
Dave    0.055411    -0.43949    0.07381 1.23094 1.37189
Ed  -0.633296   -0.50924    0.72826 0.24477 -1.45152
Frank   0.080072    -2.25261    -0.95626    2.24808 -1.06448
George  -0.665507   -0.79078    -0.67343    -1.39870    1.68714
Harry   0.258274    -0.25491    -0.87091    -0.77640    1.64443
Igor    -0.267720   -1.13759    -0.94302    0.03543 1.35381
Jeff    0.114809    0.77359 0.35249 -0.27141    0.74646


The objective here is to match up 6 names that will result in the highest total sum across all 5 variables.

Conceptually one could sum across the rows, do a descending sort, and pick the first six names.

I'm wondering how one might set this up in Mathematica, though.

Your knowledge and suggestions are greatly appreciated.

• You've been a member for almost 5 years. Surely, you could follow your own suggestion ("...one could sum across the rows, do a decending sort, and pick the first six names") and provide some attempts at the Mathematica code.
– JimB
Mar 8, 2017 at 23:44
• For 5 years I've been wondering about this, @JimBaldwin. And so many other such questions I'd like to ask. Mar 9, 2017 at 3:04
• You just gotta try it. I've had a Mathematica license since version 1 and finally in the last few years I've started to use some of the more subtle and useful features. Much of my progress has come from trying things and reading and trying many things from this site. And my sarcastic remark above is probably way extreme for this site. Folks here are really, really helpful. Just show some effort and you'll get lots of help.
– JimB
Mar 9, 2017 at 3:33
• This little problem apparently is harder than I was imagining. No one is stepping up to the plate. Go figure.... Mar 14, 2017 at 23:30
• Please look up Import, Total, Sort(By), Part. The hardest part is probably the import part, however, without the file/format given, I am unable to help more. Mar 20, 2017 at 7:12

file = "Name    x1  x2  x3  x4  x5
Abe -0.539010   0.75445 -1.44697    -0.06767    1.69325
Bob -0.992619   -2.20993    1.73367 2.01081 -0.94835
Carl    -0.821359   1.08176 0.24655 0.66602 0.20304
Dave    0.055411    -0.43949    0.07381 1.23094 1.37189
Ed  -0.633296   -0.50924    0.72826 0.24477 -1.45152
Frank   0.080072    -2.25261    -0.95626    2.24808 -1.06448
George  -0.665507   -0.79078    -0.67343    -1.39870    1.68714
Harry   0.258274    -0.25491    -0.87091    -0.77640    1.64443
Igor    -0.267720   -1.13759    -0.94302    0.03543 1.35381
Jeff    0.114809    0.77359 0.35249 -0.27141    0.74646";

data = SemanticImportString @ StringReplace[file, " " .. -> " "]


top = MaximalBy[data, Total @* Rest, UpTo[6] ]


Normal @ top[All, "Name"]

{"Dave", "Jeff", "Carl", "Abe", "Harry", "Bob"}