7
$\begingroup$

In R I can do:

summary(data)

Which gives the min, max, median, first, and third quartiles. Is there a similar single command in Mathematica for this?

I know I can do:

data = RandomInteger[{0, 100}, 100]
Table[f[data], {f, {Min, Quantile[#, .25] &, Median, 
   Quantile[#, .75] &, Max}}]

But wondering if there is a single command for this.

$\endgroup$
  • 1
    $\begingroup$ No, there isn't. $\endgroup$ – Stefan R Sep 11 '15 at 21:42
  • 5
    $\begingroup$ @StefanR - Actually, since Quantile accepts a list of quantiles: Quantile[data, Range[0, 1, .25]] $\endgroup$ – Bob Hanlon Sep 11 '15 at 22:46
16
$\begingroup$

Even if there wasn't one, there is one now: I think you've just written it yourself :-)

summary[dset_] := Through[{Min, Quantile[#, .25] &, Median, Quantile[#, .75] &, Max}[dset]]

SeedRandom[1234]
data = RandomInteger[{0, 100}, 100];
summary[data]

(* Out: {0, 18, 91/2, 66, 98} *)
$\endgroup$
  • $\begingroup$ Addendum: there might be a situation where one might need to fiddle with the third argument of Quantile[]. See e.g. this. $\endgroup$ – J. M. will be back soon Oct 7 '15 at 11:49
  • $\begingroup$ @J.M. ah! Interesting, thank you for pointing it out. I'm delighted to see that you're back! $\endgroup$ – MarcoB Oct 7 '15 at 12:28
13
$\begingroup$

On 2019-10-02 the resource function "RecordsSummary" was published at the Wolfram Function Repository.

Here is usage example (from the resource function page):

Block[{n = 200}, 
  arr = Flatten /@ 
    Transpose[{RandomReal[{-10, 10}, {n, 2}], 
      MapAt[ToLowerCase, 
       RandomChoice[CharacterRange["A", "Z"], {n, 2}], {All, 2}], 
      RandomChoice[{E, I, \[CapitalGamma]}, n]}]];

ResourceFunction["RecordsSummary"][arr]

enter image description here

First answer (2016-02-25)

Some time ago I programmed such a function, RecordsSummary, inspired by R's summary. Here is an example of its usage: Census data summary .

As the name implies, it is assumed that we have a list of records, all with the same length, and we want the columns to be summarized. (Each record is a row.)

You can get the package MathematicaForPredictionUtilities.m from MathematicaForPrediction at GitHub or simply run this command:

Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/MathematicaForPredictionUtilities.m"]

Let us create random data.

data = RandomInteger[{0, 100}, 100];
dataCat = RandomChoice[Characters["azbuka"], 100];
data2 = RandomInteger[{0, 100}, {100, 4}];

Here are examples of using RecordsSummary over the created data.

1. Call on a 1D array:

RecordsSummary[ data ]

2. Summary of a 2D numeric array. The columns are named automatically.

RecordsSummary[N[data2]]

RecordsSummary output for 2D numerical array

3. Fancy output of numerical and categorical data summary. The column names are the second argument given to RecordsSummary.

Grid[{RecordsSummary[
   Transpose[{N[data], dataCat}], {"Numeric", "Categorical"}]}, 
 Alignment -> Top, Dividers -> All]

RecordsSummary output for numerical and categorical data

$\endgroup$
  • $\begingroup$ Can we have Recordsummary with respective to two variables that is, a response variable that is categorical and a predictor variable that is numeric? $\endgroup$ – Srajan Dec 1 at 23:07
  • $\begingroup$ @Srajan I am not sure what do you meant. The answer is "yes" to both interpretations I can come up with. 1. If you have two columns, a categorical and a numerical one, then see the last example in this answer. 2. If you mean an association of number-symbol pairs, then you can use ResourceFunction["RecordsSummary"][asc, Thread -> True]. $\endgroup$ – Anton Antonov Dec 2 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.