Use base R’s head() to take a quick look at the first observations in a data set or vector. It is a practical checkpoint after importing, reshaping, or creating variables: inspect a small slice before moving into analysis. With a data frame or tibble, head(data) shows the first six rows by default. section 2.3.3 section 3.3
Add a second argument when six rows are not enough—or are too many. For example, head(draft, 3) returns the first three draft records. Pair it with tail() when you need to inspect both ends of a result. section 2.3.3 section 2.5.5
Check a newly created variable
After extracting seconds from a play-length string, compare the original and derived vectors directly: head(pbp$play_length) head(pbp$play_length2) Using $ selects one variable, so the output stays focused: values such as "00:00:12" can be checked against 12. section 8.4
For a wide data frame, inspect only the columns relevant to the question. Subset columns by position inside the first argument, then set the row count: head(nba_stats[, c(1, 3, 4, 13)], 10). Here, positions—not variable names—are required for that selection expression. section 14.6.1
Validate row order after combining seasons
After creating a season variable for 300 team-season records, inspect the beginning and end to confirm the expected ordering: head(nba$season, n = 3) tail(nba$season, n = 3) The first values are 2017 and the last are 2008, supporting the intended 30-record blocks per season. section 14.2.4
head(draft, 3)
Rk Year Pk Tm Player Age Pos Born College
## <dbl> <dbl> <dbl> <chr> <chr> <dbl> <chr> <chr> <chr>
## 1 1 2009 1 LAC Blake Grif... 20.1 F us Oklaho...
## 2 2 2009 2 MEM Hasheem Th... 22.1 C tz UConn
## 3 3 2009 3 OKC James Hard... 19.3 G us Arizon...
From To G MP WS WS48
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2011 2020 622 34.8 75.2 0.167
## 2 2010 2014 224 10.5 4.8 0.099
## 3 2010 2020 826 34.3 133. 0.226
tail(draft, 3)
Rk Year Pk Tm Player Age Pos Born College
## <dbl> <dbl> <dbl> <chr> <chr> <dbl> <chr> <chr> <chr>
## 1 291 2000 27 IND Primo_ Bre... 20.3 C si 0
## 2 292 2000 28 POR Erick Bark... 22.1 G us St. Jo...
## 3 293 2000 29 LAL Mark Madsen 24.2 F us Stanfo...
From To G MP WS WS48
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 291 2002 2010 342 18.1 10.8 0.084
## 292 2001 2002 27 9.9 0.2 0.027
## 293 2001 2009 453 11.8 8.2 0.074
Inspect the first three NBA draft records alongside the last three records with head() and tail(). section 2.3.3
