Dataframes, including tibbles, can be treated as lists of column vectors, so list indexing recovers a specified column.
tbl
# A tibble: 4 × 3
languages created has.syllabus
<chr> <dbl> <lgl>
1 Fortran 1957 FALSE
2 R 1993 TRUE
3 Python 1991 TRUE
4 Julia 2012 TRUE
tbl$created
[1] 1957 1993 1991 2012
A dataframe can also be indexed with matrix-style indexing.
tbl[c(2, 4), 1:2]
# A tibble: 2 × 2
languages created
<chr> <dbl>
1 R 1993
2 Julia 2012
Using dplyr, get a single column with pull() with the name or sequential number (negative numbers to count right-to-left).
tbl |>pull(created)
[1] 1957 1993 1991 2012
This is the same result as tbl$created, but using a pipeline-friendly function.
To get multiple columns, the appropriate function is select(), which is highly versatile. Get (or drop) columns based on properties of their name or type.
# Range with position and/or nametbl |>select(1:created)
# A tibble: 4 × 2
languages created
<chr> <dbl>
1 Fortran 1957
2 R 1993
3 Python 1991
4 Julia 2012
# Exclude a columntbl |>select(!created)
# A tibble: 4 × 2
languages has.syllabus
<chr> <lgl>
1 Fortran FALSE
2 R TRUE
3 Python TRUE
4 Julia TRUE
# Use type of columntbl |>select(where(is.numeric))
# A tibble: 4 × 1
created
<dbl>
1 1957
2 1993
3 1991
4 2012
Multiple criteria are allowed, using Boolean operators &, | and ! (and, or not).
Column names that are valid R identifiers do not need quotes within a select(). Invalid names (e.g. those including spaces) can be enclosed in backticks, though renaming them might be better.
# limit display to top 3 rows of non-list columnsstarwars |>select(!where(is.list)) |>head(3)
# A tibble: 3 × 11
name height mass hair_color skin_color eye_color birth_year sex gender
<chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
1 Luke Sky… 172 77 blond fair blue 19 male mascu…
2 C-3PO 167 75 <NA> gold yellow 112 none mascu…
3 R2-D2 96 32 <NA> white, bl… red 33 none mascu…
# ℹ 2 more variables: homeworld <chr>, species <chr>
# pick a subset of columnsstarwars |>select(name |ends_with("color")) |>head(5)
# A tibble: 5 × 4
name hair_color skin_color eye_color
<chr> <chr> <chr> <chr>
1 Luke Skywalker blond fair blue
2 C-3PO <NA> gold yellow
3 R2-D2 <NA> white, blue red
4 Darth Vader none white yellow
5 Leia Organa brown light brown
Get rows matching some criteria with filter(), or exclude them with filter_out().
arrange() sorts rows by the values in one or more columns.
tbl
# A tibble: 4 × 3
languages created has.syllabus
<chr> <dbl> <lgl>
1 Fortran 1957 FALSE
2 R 1993 TRUE
3 Python 1991 TRUE
4 Julia 2012 TRUE
tbl |>arrange(languages)
# A tibble: 4 × 3
languages created has.syllabus
<chr> <dbl> <lgl>
1 Fortran 1957 FALSE
2 Julia 2012 TRUE
3 Python 1991 TRUE
4 R 1993 TRUE
data.table
Polars
TODO
DuckDB
TODO
Arrow
TODO
Modifying
data.frame & tibble
Column names can be changed with rename(newname = oldname), or rename_with() to apply a function. A typical use would be cleaning up imported names to make them easier to work with in R, by removing whitespace and forcing a consistent format for related names.
Note the syntax within rename(). The contents of column oldname are bound to name newname, hence the order.
Column order can be changed with relocate(). Specified column(s) are moved to the left-most position(s) by default, but a .before or .after argument can be used for finer positioning.
# A tibble: 4 × 5
name species height mass BMI
<chr> <chr> <int> <dbl> <dbl>
1 Luke Skywalker Human 172 77 26.0
2 C-3PO Droid 167 75 26.9
3 R2-D2 Droid 96 32 34.7
4 Darth Vader Human 202 136 33.3
When you want to operate on a subset of the columns with functions such as mutate(), the select() |> mutate() sequence in the above example is one option. Only the selected columns will be in the result.
Alternatively, it can be convenient to use pick()within the mutate() call:
# A tibble: 4 × 15
name height mass hair_color skin_color eye_color birth_year sex gender
<chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
1 Luke Sky… 172 77 blond fair blue 19 male mascu…
2 C-3PO 167 75 <NA> gold yellow 112 none mascu…
3 R2-D2 96 32 <NA> white, bl… red 33 none mascu…
4 Darth Va… 202 136 none white yellow 41.9 male mascu…
# ℹ 6 more variables: homeworld <chr>, species <chr>, films <list>,
# vehicles <list>, starships <list>, BMI <dbl>
When you want to operate on a subset of the columns with functions such as mutate(), the select() |> mutate() sequence in the above example is one option. Only the selected columns will be in the result.
Alternatively, it can be convenient to use pick()within the mutate() call: