liveBook · Manning

Square brackets

As explained in Statistics Slam Dunk

An extraction operator is syntax that selects a subset of elements from an R vector, matrix, array, list, or data frame.

Square brackets are R’s extraction operators: they index a vector, matrix, array, list, or data frame to select a subset of elements. They can also identify values that need correction before analysis. section 11.4.1 section 13.5

Use square brackets to answer practical data-wrangling questions:

  • Select columns or column ranges from a data frame, such as positions 2 through 17 before calculating salary totals. section 11.4.1
  • Select vector elements meeting a condition, such as win shares below zero. section 13.5
  • Modify the selected elements directly, replacing negative win shares with zero. section 13.5

Correcting negative win shares

The expression `ws_gini2$WS[ws_gini2$WS < 0] = 0.0` selects every `WS` value below zero and assigns it zero. Calling `min(ws_gini2$WS)` afterward returns 0, confirming the correction. section 13.5

ws_gini2$WS[ws_gini2$WS < 0] = 0.0
min(ws_gini2$WS)
## [1] 0

Inspect the conditional-indexing and replacement operation directly. section 13.5

How Statistics Slam Dunk covers it

How deep it goes
Defines extraction operators and demonstrates positional selection, conditional selection, indexed assignment, and validation.
Examples
Demonstrates selecting salary and win columns, calculating team summaries, and replacing 227 negative win-share records.
What you can do after
Diagnose and correct selected R values while extracting columns for practical NBA data summaries.
How it connects
Connects indexing with vectors, data frames, rowSums(), dplyr, data cleaning, and Lorenz-curve preparation.

Also explained in

Compare how 2 books explain square brackets →

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.