liveBook · Manning

Dim() function

As explained in Statistics Slam Dunk

The dim() function returns the dimensions of an R object, such as the number of rows and columns in a data frame.

`dim()` is a base R function that returns an object's dimensions. For a data frame, the result lists the number of rows first and the number of columns second. It is a fast way to confirm that an import produced the expected data shape before analysis begins. section 2.2 section 6.2

Check a newly imported draft data set

After importing `draft.csv`, run `dim(draft)`. The output `293 26` means the data contains 293 records and 26 variables. That immediate check confirms that R received a data set of the expected size. section 2.2

dim(draft)
## [1] 293  26

The two returned values give row count followed by column count. section 2.2

Use `dim()` again after a wrangling step to verify what changed. Removing two poorly tracked variables from `hustle` leaves its 90 rows intact while reducing its width from 14 columns to 12. section 5.3 section 15.3

Dimension checks make common data operations easier to verify:

  • After `filter()`, confirm that unwanted records were removed while the needed variables remain. section 15.3
  • After `select()`, confirm that the number of columns fell as intended. section 6.3.2
  • After `rbind()`, confirm that rows from matching data sets were stacked while the column count stayed the same. section 14.2.4
  • After `cbind()` or `left_join()`, confirm that matched data sets have the expected row and column counts. section 7.3 section 6.3.2
  • After splitting data for modeling, confirm the sizes of training and test sets. section 14.6.1

Compare two NBA seasons

Calling `dim(nba1819)` and `dim(nba1920)` shows that both data sets have 57 variables, but the 2019–20 data has fewer observations. The row difference reflects a shortened season, while the matching column counts show the files share the same structure. section 6.2

`dim()` reports shape only; it does not show variable names, types, or sample values. Pair it with `glimpse()` for a transposed, truncated view of a data set, or with `head()` and `tail()` when you also want to inspect records. section 3.3 section 14.2.4

How Statistics Slam Dunk covers it

How deep it goes
Defines row-first and column-second output and applies it repeatedly, without detailing implementation internals.
Examples
Uses NBA draft, box-score, hustle, payroll, play-by-play, salary, and team-season data sets.
What it assumes
Introduces dimensions through imported data frames and explains rows as observations and columns as variables.
What you can do after
Apply it via base R to verify data-set shape after importing, filtering, selecting, joining, or splitting records.
How it connects
Connects to read_csv(), select(), filter(), rbind(), cbind(), left_join(), glimpse(), head(), and tail().

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Statistics Slam Dunk. The section links point to the parts of the book it draws on.