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

