Why Use summary()?
After inspecting a data frame’s structure and columns, a useful next step is to get a compact overview of the values in each column. In base R, summary() applies a summary appropriate to each column’s class. This makes one call useful for a first pass over a mixed data frame: numeric columns show numerical summaries, factors show level counts, and logical columns show counts of TRUE and FALSE.
The output is descriptive, not diagnostic. It can reveal missing values, unusual ranges, or unexpected levels, but it does not determine whether a value is valid in context, whether a variable was recorded in the correct units, or whether every required data check has been completed. This distinction is important in predictive analytics, where a plausible-looking summary is not proof that input data are fit for modeling.
Reading a Numeric Summary
For a numeric column, the main summary values describe location and spread. The minimum and maximum are the smallest and largest observed nonmissing values. The quartiles and median describe positions in the sorted data. The mean is the arithmetic average of the nonmissing values. In base R’s usual summary of numeric data, missing observations are excluded from these statistics and their number is reported separately as NA’s.
The first and third quartiles in the default R summary use the usual type 7 sample quantile convention. You do not need to calculate every quartile by hand to use the output, but knowing how the default is formed helps explain an intermediate value. For \(n\) sorted nonmissing observations, type 7 locates the quantile at probability \(p\) using position \(h=1+(n-1)p\). If \(h\) is not an integer, R interpolates between the values on either side of that position.
For example, with six nonmissing values, the first-quartile position is \(1+5(0.25)=2.25\). The result is one-quarter of the way from the second sorted value to the third. The median for an even number of observations is halfway between the two middle values. These are conventions for interpreting the displayed summary, not a substitute for considering the data’s context.
Worked Example: Summarizing a Mixed Data Frame
A hypothetical analyst inspects seven application records. The data frame has a numeric requested amount, a factor for application channel, and a logical indicator for whether a document was reviewed. The values are: amounts 100, 200, 300, 400, 500, 500, and missing; channels Web, Agent, Web, Broker, Agent, Web, Broker; and review indicators TRUE, FALSE, TRUE, missing, FALSE, TRUE, FALSE. The channel factor has levels Web, Agent, Broker, and Phone. What should the analyst learn from summary(df)?
Method. Apply the class-specific summary to each column, then interpret each output separately. For the amount, first set aside the missing observation. The six observed values are already in sorted order: 100, 200, 300, 400, 500, 500. Their sum is \(100+200+300+400+500+500=2{,}000\), so the mean is \(2{,}000/6=333.333\ldots\), displayed approximately as 333.3 under typical printing settings.
For the first quartile, \(h=1+(6-1)(0.25)=2.25\), so interpolate between 200 and 300: \(200+0.25(300-200)=225\). The median is the average of the third and fourth values: \((300+400)/2=350\). For the third quartile, \(h=1+5(0.75)=4.75\), giving \(400+0.75(500-400)=475\). The minimum is 100, the maximum is 500, and there is one missing amount.
Numeric output. The amount summary therefore reports Min. 100, 1st Qu. 225, Median 350, Mean approximately 333.3, 3rd Qu. 475, Max. 500, and NA’s 1. The mean uses six observed amounts, not seven records. Dividing by seven would incorrectly treat the missing amount as if it were a zero.
Factor output. Web occurs three times, Agent twice, and Broker twice. Phone is an allowed factor level but occurs zero times. The factor summary reports those level counts, including the unused Phone level. These are counts, not proportions, and the ordering of the printed levels does not establish that the categories have a meaningful ranking.
Logical output. There are three TRUE values, three FALSE values, and one missing value. A logical summary reports the counts by value; it does not tell the analyst why the review status is missing.
Check. The three columns account for seven records: six observed amounts plus one missing amount; seven channel values; and six observed logical values plus one missing value. A common mistake is to divide the amount total by all seven rows. The correction is to divide by the six nonmissing numeric values, giving \(333.3\) to one decimal place.
How the Output Follows from the Class
The same call can produce different-looking results because R uses the column’s class to decide what makes sense to summarize. A factor represents categorical values, so its summary is a count of observations at each level. A logical variable has two observed values, TRUE and FALSE, so its summary gives those counts. A numeric variable supports numerical summaries such as a mean and quartiles.
| Column class | Typical summary information | Interpretation to keep in mind |
|---|---|---|
| Numeric or integer | Minimum, quartiles, median, mean, maximum, and possibly NA’s | Statistics are calculated from observed numeric values; check whether their units and ranges make sense. |
| Factor | Counts for factor levels, including levels with zero observations when shown | Counts describe recorded categories; a level’s presence does not mean it occurs in the data. |
| Logical | Counts for FALSE, TRUE, and possibly NA’s | Missing is distinct from either FALSE or TRUE. |
For factors with many levels, the printed summary may be shortened rather than display every level individually. If a category’s count is important, inspect the output carefully and use a dedicated frequency table when appropriate. That is a separate task from interpreting the compact overall summary.
A short justification explains why a numeric mean excludes missing values in the displayed summary. The arithmetic mean is defined over the values that are available for calculation. If \(x_1,\ldots,x_m\) are the \(m\) observed numeric values, their mean is the total of those values divided by \(m\). A missing entry is not a numerical zero and cannot be included as an addend or as an additional observation in that calculation. R therefore summarizes the observed values and reports the missing count separately.
This reasoning depends on distinguishing missingness from a recorded zero. If zero is a valid observed value, it is included in the numeric statistics. If a data system uses a special numeric code to stand for “not recorded,” R will treat that code as an ordinary number unless it has been converted to a missing value or otherwise handled. The summary cannot infer the intended meaning of a code.
Worked Example: Interpreting Factor and Logical Counts
A hypothetical service team records the status and automatic-payment flag for eight accounts. Status is a factor with levels Active, Closed, Pending, and Withdrawn; the recorded statuses are Active, Active, Closed, Pending, Closed, Active, missing, Pending. The logical automatic-payment values are TRUE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE, missing. Interpret the categorical and logical summaries.
Method. Count each recorded factor level, keeping any unused level separate from missingness. Active occurs in records 1, 2, and 6, so its count is 3. Closed occurs in records 3 and 5, so its count is 2. Pending occurs in records 4 and 8, so its count is 2. Withdrawn never occurs, so its count is 0. One status is missing. A factor summary therefore shows counts 3, 2, 2, and 0 for those levels, as well as the missing count when printed by the R method in use.
For the logical column, TRUE occurs in records 1, 2, 4, and 7, giving 4. FALSE occurs in records 3, 5, and 6, giving 3. Record 8 is missing, giving 1 missing value. The logical summary therefore reports FALSE 3, TRUE 4, and NA’s 1.
Check. The status counts total \(3+2+2+0=7\) observed values, plus one missing value, for eight records. The logical counts total \(3+4=7\) observed values, plus one missing value. The zero for Withdrawn means that the level exists in the factor definition but has no observed records; it is not a missing status.
Use the Summary as a First Check, Not a Verdict
A summary can help direct further inspection. A very large maximum, a negative minimum for a variable that should be nonnegative, or many missing values may warrant investigation. Factor levels that look unexpected may indicate inconsistent data entry or an unsuitable class. These are signals to ask a question, not proof that a value is erroneous.
A compact summary does not show every observation, the order of records, relationships between columns, or whether a value meets a business rule. Nor does a numeric summary establish that a numeric column is truly quantitative: a column of category codes stored as numbers will receive numeric statistics even when averaging those codes is meaningless. Class inspection and contextual knowledge remain necessary.
Worked Example: A Summary That Reveals a Possible Sentinel Code
A hypothetical analyst examines service duration in days. The system requires durations to be nonnegative, but the numeric column contains 12, 14, 15, 17, 20, 22, and -999. The data dictionary has not yet been checked. Calculate and interpret the numeric summary, using the default type 7 quartiles.
Method. Treat all seven entries as numeric observations for the calculation, because -999 has not been coded as missing. Sorted, they are -999, 12, 14, 15, 17, 20, 22. The sum of the six nonnegative values is \(12+14+15+17+20+22=100\); adding -999 gives \(-899\). The mean is \(-899/7=-128.428571\ldots\), or approximately -128.4 to one decimal place.
The minimum is -999 and the maximum is 22. With \(n=7\), the first-quartile position is \(1+6(0.25)=2.5\), halfway between -999 and 12, so the first quartile is \((-999+12)/2=-493.5\). The median is the fourth sorted value, 15. The third-quartile position is \(1+6(0.75)=5.5\), halfway between 17 and 20, so the third quartile is 18.5. There are no R missing values, so the summary does not report an NA count.
Because durations are required to be nonnegative, -999 is suspicious and should be checked against the data dictionary or source process. But the summary alone cannot establish that it is a missing-value code; it might be a data-entry error or another coding convention. The analyst should confirm its meaning before recoding or excluding it.
Check. The mean, minimum, and first quartile are all strongly affected by -999. A common mistake is to assume that an extreme value will automatically be treated as missing. It will not: a numeric sentinel remains an ordinary number until the analyst identifies and handles it appropriately.
Interpret the output in light of whether each column is numeric, factor, or logical; a class can change what its summary means.
For numeric columns, review range, quartiles, mean, and missing count. For factors and logical columns, review category or truth-value counts.
Investigate suspicious values, unexpected levels, or missingness using the variable definition and data rules. Do not assume summary() has validated the data.
Common Mistakes
- Dividing by the number of rows when values are missing. A numeric mean is based on observed numeric values, not on missing entries treated as zero.
- Confusing an unused factor level with a missing value. An unused level has zero observed cases; a missing value has no recorded category.
- Reading logical NA as FALSE. Missing means the status is unknown or unrecorded, not that the answer is FALSE.
- Assuming an extreme value is automatically excluded. A numeric code such as -999 is included unless it has actually been represented as missing or otherwise handled.
- Treating the output as a complete quality check. Summary statistics do not verify definitions, units, row-level rules, or relationships between fields.
Check Your Understanding
For each item, identify what the summary() output means and note any limitation stated in the question.
- A numeric column contains 8, 10, 12, 14, and one missing value. What are its mean, median, minimum, maximum, and missing count? (Answer: Mean 11, median 11, minimum 8, maximum 14, NA’s 1.)
- A factor has levels Low, Medium, and High, with recorded values Low, High, High, missing, and Low. What counts should its summary convey, including the unused level? (Answer: Low 2, Medium 0, High 2, and 1 missing value.)
- A logical column contains TRUE, FALSE, TRUE, FALSE, FALSE, and missing. What are the FALSE, TRUE, and missing counts? (Answer: FALSE 3, TRUE 2, NA’s 1.)
- A numeric column stores the codes 1, 1, 2, and 3 for three unordered categories, with code 1 appearing twice. Why is its numeric summary potentially misleading, and what should be checked? (Answer: The numeric class leads R to compute numeric statistics, but the codes may be category labels for which arithmetic is meaningless; check the variable definition and whether it should be represented as a factor.)
- A duration column, known to be nonnegative, contains -99, 4, 5, 7, and 9. Using default type 7 quartiles, what are the mean, first quartile, median, third quartile, minimum, and maximum, and what should be investigated? (Answer: Mean -14.8, first quartile 4, median 5, third quartile 7, minimum -99, maximum 9; investigate whether -99 is an invalid value or a sentinel code, since summary() treats it as numeric rather than automatically as missing.)