R fundamentals

Author

Tobias Gerstenberg, adapted by Bria Long

Published

Invalid Date

Data wrangling 1

This notebook takes a look at how to wrangle data using the dplyr package. The nice thing about R is that (thanks to the tidyverse), both visualization and data wrangling are particularly powerful. Many analysis pipelines use both Python and R. However, I often like to use R for data visualization and wrangling for tabular data, which is common in experimental psychology.

Learning goals

  • Review R basics (incl. variable modes, data types, operators, control flow, and functions).
  • Learn how the pipe operator %>% works.
  • See different ways for getting a sense of one’s data.
  • Master key data manipulation verbs from the dplyr package (incl. filter(), arrange(), rename(), relocate(), select(), mutate()) as well as the helper functions across() and where().

Load packages

Let’s first load the packages that we need for this chapter.

library("knitr")        # for rendering the RMarkdown file
library("skimr")        # for visualizing data 
library("visdat")       # for visualizing data 
library("DT")           # for visualizing data 
library("tidyverse")    # for data wrangling

Some R basics

To test your knowledge of the R basics, I recommend taking the free interactive tutorial on datacamp: Introduction to R. Here, I will just give a very quick overview of some of the basics.

Modes

Variables in R can have different modes. Table @ref(tab:variable-modes) shows the most common ones.

Table 1: Most commonly used data types in Python.
Most commonly used variable modes in R.
name example
numeric 1, 3, 48
character 'Steve', 'a', '78'
logical TRUE, FALSE
not available NA

For characters you can either use " or '. R has a number of functions to convert a variable from one mode to another. NA is used for missing values.

tmp1 = "1" # we start with a character
str(tmp1) 

tmp2 = as.numeric(tmp1) # turn it into a numeric
str(tmp2) 

tmp3 = as.factor(tmp2) # turn that into a factor
str(tmp3)

tmp4 = as.character(tmp3) # and go full cycle by turning it back into a character
str(tmp4)

identical(tmp1, tmp4) # checks whether tmp1 and tmp4 are the same

The str() function displays the structure of an R object. Here, it shows us what mode the variable is.

Data types

R has a number of different data types. Table @ref(tab:data-types) shows the ones you’re most likely to come across (taken from this source):

Table 2: Most commonly used data types in R.
name description
vector list of values with of the same variable mode
factor for ordinal variables
matrix 2D data structure
array same as matrix for higher dimensional data
data frame similar to matrix but with column names
list flexible type that can contain different other variable types

Vectors

We build vectors using the concatenate function c(), and we use [] to access one or more elements of a vector.

numbers = c(1, 4, 5) # make a vector
numbers[2] # access the second element 
[1] 4
numbers[1:2] # access the first two elements
[1] 1 4
numbers[c(1, 3)] # access the first and last element
[1] 1 5

In R (unlike in Python for example), 1 refers to the first element of a vector (or list).

Matrix

We build a matrix using the matrix() function, and we use [] to access its elements.

matrix = matrix(data = c(1, 2, 3, 4, 5, 6),
                nrow = 3,
                ncol = 2)
matrix # the full matrix
     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6
matrix[1, 2] # element in row 1, column 2
[1] 4
matrix[1, ] # all elements in the first row 
[1] 1 4
matrix[ , 1] # all elements in the first column 
[1] 1 2 3
matrix[-1, ] # a matrix which excludes the first row
     [,1] [,2]
[1,]    2    5
[2,]    3    6

Note how we use an empty placeholder to indicate that we want to select all the values in a row or column, and - to indicate that we want to remove something.

Array

Arrays work the same was as matrices with data of more than two dimensions.

Data frame

df = tibble(participant_id = c(1, 2, 3),
            participant_name = c("Leia", "Luke", "Darth")) # make the data frame 

df # the complete data frame
participant_id participant_name
1 Leia
2 Luke
3 Darth
df[1, 2] # a single element using numbers 
participant_name
Leia
df$participant_id # all participants 
[1] 1 2 3
df[["participant_id"]] # same as before but using [[]] instead of $
[1] 1 2 3
df$participant_name[2] # name of the second participant
[1] "Luke"
df[["participant_name"]][2] # same as above
[1] "Luke"

We’ll use data frames a lot. Data frames are like a matrix with column names. Data frames are also more general than matrices in that different columns can have different modes. For example, one column might be a character, another one numeric, and another one a factor.

Here we used the tibble() function to create the data frame. A tibble is almost the same as a data frame but it has better defaults for formatting output in the console (more information on tibbles is here).

Lists

l.mixed = list(number = 1, 
               character = "2", 
               factor = factor(3), 
               matrix = matrix(1:4, ncol = 2),
               df = tibble(x = c(1, 2), y = c(3, 4)))
l.mixed
$number
[1] 1

$character
[1] "2"

$factor
[1] 3
Levels: 3

$matrix
     [,1] [,2]
[1,]    1    3
[2,]    2    4

$df
# A tibble: 2 × 2
      x     y
  <dbl> <dbl>
1     1     3
2     2     4
# three different ways of accessing a list
l.mixed$character
[1] "2"
l.mixed[["character"]]
[1] "2"
l.mixed[[2]] 
[1] "2"

Lists are a very flexible data format. You can put almost anything in a list.

Operators

Table @ref(tab:logical-operators) shows the comparison operators that result in logical outputs.

Table 3: Table of comparison operators that result in boolean (TRUE/FALSE) outputs.
symbol name
== equal to
!= not equal to
>, < greater/less than
>=, <= greater/less than or equal
&, &#124;, ! logical operators: and, or, not
%in% checks whether an element is in an object

The %in% operator is very useful, and we can use it like so:

x = c(1, 2, 3)
2 %in% x 
[1] TRUE
c(3, 4) %in% x
[1]  TRUE FALSE

It’s particularly useful for filtering data as we will see below.

Control flow

if-then

number = 3

if (number == 1) {
  print("The number is 1.")
} else if (number == 2) {
  print("The number is 2.")
} else {
  print("The number is neither 1 nor 2.")
}
[1] "The number is neither 1 nor 2."

As a shorthand version, we can also use the ifelse() function like so:

number = 3
ifelse(test = number == 1, yes = "correct", no = "false")
[1] "false"

for loop

sequence = 1:10

for(i in 1:length(sequence)){
  print(i)
}
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10

while loop

number = 1 

while(number <= 10){
  print(number)
  number = number + 1
}
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10

Functions

fun.add_two_numbers = function(a, b){
  x = a + b
  return(str_c("The result is ", x))
}

fun.add_two_numbers(1, 2)
[1] "The result is 3"

I’ve used the str_c() function here to concatenate the string with the number. (R converts the number x into a string for us.) Note, R functions can only return a single object. However, this object can be a list (which can contain anything).

Some often used functions

Table 4: Some frequently used functions.
name description
length() length of an object
dim() dimensions of an object (e.g. number of rows and columns)
rm() remove an object
seq() generate a sequence of numbers
rep() repeat something n times
max() maximum
min() minimum
which.max() index of the maximum
which.min() index of the maximum
mean() mean
median() median
sum() sum
var() variance
sd() standard deviation

The pipe operator %>%

Inspiration for the magrittr package name.

The magrittr package logo.

The pipe operator %>% is a special operator introduced in the magrittr package. It is used heavily in the tidyverse. The basic idea is simple: this operator allows us to “pipe” several functions into one long chain that matches the order in which we want to do stuff.

Let’s consider the following example of making and eating a cake (thanks to https://twitter.com/dmi3k/status/1191824875842879489?s=09). This would be the traditional way of writing some code (this code block doesn’t get evaluated :)

eat(
  slice(
    bake(
      put(
        pour(
          mix(ingredients),
          into = baking_form),
        into = oven),
      time = 30),
    pieces = 6),
  1)

To see what’s going on here, we need to read the code inside out. That is, we have to start in the innermost bracket, and then work our way outward. However, there is a natural causal ordering to these steps and wouldn’t it be nice if we could just write code in that order? Thanks to the pipe operator %>% we can! Here is the same example using the pipe:

ingredients %>%
  mix %>%
  pour(into = baking_form) %>%
  put(into = oven) %>%
  bake(time = 30) %>%
  slice(pieces = 6) %>%
  eat(1)

This code is much easier to read and write, since it represents the order in which we want to do things!

Abstractly, the pipe operator does the following:

f(x) can be rewritten as x %>% f()

For example, in standard R, we would write:

x = 1:3

# standard R 
sum(x)
[1] 6

With the pipe, we can rewrite this as:

x = 1:3

# with the pipe  
x %>% sum()
[1] 6

This doesn’t seem super useful yet, but just hold on a little longer.

f(x, y) can be rewritten as x %>% f(y)

So, we could rewrite the following standard R code …

# rounding pi to 6 digits, standard R 
round(pi, digits = 6)
[1] 3.141593

… by using the pipe:

# rounding pi to 6 digits, standard R 
pi %>% round(digits = 6)
[1] 3.141593

Here is another example:

a = 3
b = 4
sum(a, b) # standard way 
[1] 7
a %>% sum(b) # the pipe way 
[1] 7

The pipe operator inserts the result of the previous computation as a first element into the next computation. So, a %>% sum(b) is equivalent to sum(a, b). We can also specify to insert the result at a different position via the . operator. For example:

a = 1
b = 10 
b %>% seq(from = a, to = .)
 [1]  1  2  3  4  5  6  7  8  9 10

Here, I used the . operator to specify that I woud like to insert the result of b where I’ve put the . in the seq() function.

f(x, y) can be rewritten as y %>% f(x, .)

Still not to thrilled about the pipe? We can keep going though (and I’m sure you’ll be convinced eventually.)

h(g(f(x))) can be rewritten as x %>% f() %>% g() %>% h()

For example, consider that we want to calculate the root mean squared error (RMSE) between prediction and data.

Here is how the RMSE is defined:

\[ \text{RMSE} = \sqrt\frac{\sum_{i=1}^n(\hat{y}_i-y_i)^2}{n} \] where \(\hat{y}_i\) denotes the prediction, and \(y_i\) the actually observed value.

In base R, we would do the following.

data = c(1, 3, 4, 2, 5)
prediction = c(1, 2, 2, 1, 4)

# calculate root mean squared error
rmse = sqrt(mean((prediction-data)^2))
print(rmse)
[1] 1.183216

Using the pipe operator makes the operation more intuitive:

data = c(1, 3, 4, 2, 5)
prediction = c(1, 2, 2, 1, 4)

# calculate root mean squared error the pipe way 
rmse = (prediction-data)^2 %>% 
  mean() %>% 
  sqrt() %>% 
  print() 
[1] 1.183216

First, we calculate the squared error, then we take the mean, then the square root, and then print the result.

The pipe operator %>% is similar to the + used in ggplot2. It allows us to take step-by-step actions in a way that fits the causal ordering of how we want to do things.

Tip: The keyboard shortcut for the pipe operator is:
cmd/ctrl + shift + m
Definitely learn this one – we’ll use the pipe a lot!!

Tip: Code is generally easier to read when the pipe %>% is at the end of a line (just like the + in ggplot2).

A key advantage of using the pipe is that you don’t have to save intermediate computations as new variables and this helps to keep your environment nice and clean!

Practice 1

Let’s practice the pipe operator.

# here are some numbers
x = seq(from = 1, to = 5, by = 1)

# taking the log the standard way
log(x)
[1] 0.0000000 0.6931472 1.0986123 1.3862944 1.6094379
# now take the log the pipe way (write your code underneath)
# some more numbers
x = seq(from = 10, to = 5, by = -1)

# the standard way
mean(round(sqrt(x), digits = 2))
[1] 2.721667
# the pipe way (write your code underneath)

A quick note on naming things

Personally, I like to name things in a (pretty) consistent way so that I have no trouble finding stuff even when I open up a project that I haven’t worked on for a while. I try to use the following naming conventions:

Table 5: Some naming conventions I adopt to make my life easier.
name use
df.thing for data frames
l.thing for lists
fun.thing for functions
tmp.thing for temporary variables

Looking at data

The package dplyr which we loaded as part of the tidyverse, includes a data set with information about starwars characters. Let’s store this as df.starwars.

df.starwars = starwars

Note: Unlike in other languages (such as Python or Matlab), a . in a variable name has no special meaning and can just be used as part of the name. I’ve used df here to indicate for myself that this variable is a data frame. Before visualizing the data, it’s often useful to take a quick direct look at the data.

There are several ways of taking a look at data in R. Personally, I like to look at the data within RStudio’s data viewer. To do so, you can:

  • click on the df.starwars variable in the “Environment” tab
  • type View(df.starwars) in the console
  • move your mouse over (or select) the variable in the editor (or console) and hit F2

I like the F2 route the best as it’s fast and flexible.

Sometimes it’s also helpful to look at data in the console instead of the data viewer. Particularly when the data is very large, the data viewer can be sluggish.

Here are some useful functions:

glimpse()

glimpse() is helpful when the data frame has many columns. The data is shown in a transposed way with columns as rows.

glimpse(df.starwars)
Rows: 87
Columns: 14
$ name       <chr> "Luke Skywalker", "C-3PO", "R2-D2", "Darth Vader", "Leia Or…
$ height     <int> 172, 167, 96, 202, 150, 178, 165, 97, 183, 182, 188, 180, 2…
$ mass       <dbl> 77.0, 75.0, 32.0, 136.0, 49.0, 120.0, 75.0, 32.0, 84.0, 77.…
$ hair_color <chr> "blond", NA, NA, "none", "brown", "brown, grey", "brown", N…
$ skin_color <chr> "fair", "gold", "white, blue", "white", "light", "light", "…
$ eye_color  <chr> "blue", "yellow", "red", "yellow", "brown", "blue", "blue",…
$ birth_year <dbl> 19.0, 112.0, 33.0, 41.9, 19.0, 52.0, 47.0, NA, 24.0, 57.0, …
$ sex        <chr> "male", "none", "none", "male", "female", "male", "female",…
$ gender     <chr> "masculine", "masculine", "masculine", "masculine", "femini…
$ homeworld  <chr> "Tatooine", "Tatooine", "Naboo", "Tatooine", "Alderaan", "T…
$ species    <chr> "Human", "Droid", "Droid", "Human", "Human", "Human", "Huma…
$ films      <list> <"A New Hope", "The Empire Strikes Back", "Return of the J…
$ vehicles   <list> <"Snowspeeder", "Imperial Speeder Bike">, <>, <>, <>, "Imp…
$ starships  <list> <"X-wing", "Imperial shuttle">, <>, <>, "TIE Advanced x1",…

distinct()

distinct() shows all the distinct values for a character or factor column.

df.starwars %>% 
  distinct(species)
species
Human
Droid
Wookiee
Rodian
Hutt
NA
Yoda’s species
Trandoshan
Mon Calamari
Ewok
Sullustan
Neimodian
Gungan
Toydarian
Dug
Zabrak
Twi’lek
Aleena
Vulptereen
Xexto
Toong
Cerean
Nautolan
Tholothian
Iktotchi
Quermian
Kel Dor
Chagrian
Geonosian
Mirialan
Clawdite
Besalisk
Kaminoan
Skakoan
Muun
Togruta
Kaleesh
Pau’an

count()

count() shows a count of all the different distinct values in a column.

df.starwars %>% 
  count(eye_color)
eye_color n
black 10
blue 19
blue-gray 1
brown 21
dark 1
gold 1
green, yellow 1
hazel 3
orange 8
pink 1
red 5
red, blue 1
unknown 3
white 1
yellow 11

It’s possible to do grouped counts by combining several variables.

df.starwars %>% 
  count(eye_color, gender) %>% 
  head(n = 10)
eye_color gender n
black feminine 2
black masculine 8
blue feminine 6
blue masculine 12
blue NA 1
blue-gray masculine 1
brown feminine 4
brown masculine 15
brown NA 2
dark masculine 1

datatable()

For RMardkown files specifically, we can use the datatable() function from the DT package to get an interactive table widget.

df.starwars %>% 
  DT::datatable()

Other tools for taking a quick look at data

vis_dat()

The vis_dat() function from the visdat package, gives a visual summary that makes it easy to see the variable types and whether there are missing values in the data.

visdat::vis_dat(df.starwars)

When R loads packages, functions loaded in earlier packages are overwritten by functions of the same name from later packages. This means that the order in which packages are loaded matters. To make sure that a function from the correct package is used, you can use the package_name::function_name() construction. This way, the function_name() from the package_name is used, rather than the same function from a different package.

This is why, in general, I recommend to load the tidyverse package last (since it contains a large number of functions that we use a lot).

skim()

The skim() function from the skimr package provides a nice overview of the data, separated by variable types.

skim(df.starwars)
Data summary
Name df.starwars
Number of rows 87
Number of columns 14
_______________________
Column type frequency:
character 8
list 3
numeric 3
________________________
Group variables None

Variable type: character

skim_variable n_missing complete_rate min max empty n_unique whitespace
name 0 1.00 3 21 0 87 0
hair_color 5 0.94 4 13 0 11 0
skin_color 0 1.00 3 19 0 31 0
eye_color 0 1.00 3 13 0 15 0
sex 4 0.95 4 14 0 4 0
gender 4 0.95 8 9 0 2 0
homeworld 10 0.89 4 14 0 48 0
species 4 0.95 3 14 0 37 0

Variable type: list

skim_variable n_missing complete_rate n_unique min_length max_length
films 0 1 24 1 7
vehicles 0 1 11 0 2
starships 0 1 16 0 5

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
height 6 0.93 174.60 34.77 66 167.0 180 191.0 264 ▂▁▇▅▁
mass 28 0.68 97.31 169.46 15 55.6 79 84.5 1358 ▇▁▁▁▁
birth_year 44 0.49 87.57 154.69 8 35.0 52 72.0 896 ▇▁▁▁▁

dfSummary()

The summarytools package is another great package for taking a look at the data. It renders a nice html output for the data frame including a lot of helpful information. You can find out more about this package here.

df.starwars %>%
  select(where(~ !is.list(.))) %>% # this removes all list columns
  summarytools::dfSummary() %>%
  summarytools::view()

Note: The summarytools::view() function will not show up here in the html. It generates a summary of the data that is displayed in the Viewer in RStudio.

Once we’ve taken a look at the data, the next step would be to visualize relationships between variables of interest.

Wrangling data

We use the functions in the package dplyr to manipulate our data.

filter()

filter() lets us apply logical (and other) operators (see Table @ref(tab:logical-operators)) to subset the data. Here, I’ve filtered out the masculine characters.

df.starwars %>% 
  filter(gender == "masculine")
name height mass hair_color skin_color eye_color birth_year sex gender homeworld species films vehicles starships
Luke Skywalker 172 77.0 blond fair blue 19.0 male masculine Tatooine Human A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith , The Force Awakens Snowspeeder , Imperial Speeder Bike X-wing , Imperial shuttle
C-3PO 167 75.0 NA gold yellow 112.0 none masculine Tatooine Droid A New Hope , The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith
R2-D2 96 32.0 NA white, blue red 33.0 none masculine Naboo Droid A New Hope , The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith , The Force Awakens
Darth Vader 202 136.0 none white yellow 41.9 male masculine Tatooine Human A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith TIE Advanced x1
Owen Lars 178 120.0 brown, grey light blue 52.0 male masculine Tatooine Human A New Hope , Attack of the Clones, Revenge of the Sith
R5-D4 97 32.0 NA white, red red NA none masculine Tatooine Droid A New Hope
Biggs Darklighter 183 84.0 black light brown 24.0 male masculine Tatooine Human A New Hope X-wing
Obi-Wan Kenobi 182 77.0 auburn, white fair blue-gray 57.0 male masculine Stewjon Human A New Hope , The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith Tribubble bongo Jedi starfighter , Trade Federation cruiser, Naboo star skiff , Jedi Interceptor , Belbullab-22 starfighter
Anakin Skywalker 188 84.0 blond fair blue 41.9 male masculine Tatooine Human The Phantom Menace , Attack of the Clones, Revenge of the Sith Zephyr-G swoop bike, XJ-6 airspeeder Naboo fighter , Trade Federation cruiser, Jedi Interceptor
Wilhuff Tarkin 180 NA auburn, grey fair blue 64.0 male masculine Eriadu Human A New Hope , Revenge of the Sith
Chewbacca 228 112.0 brown unknown blue 200.0 male masculine Kashyyyk Wookiee A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith , The Force Awakens AT-ST Millennium Falcon, Imperial shuttle
Han Solo 180 80.0 brown fair brown 29.0 male masculine Corellia Human A New Hope , The Empire Strikes Back, Return of the Jedi , The Force Awakens Millennium Falcon, Imperial shuttle
Greedo 173 74.0 NA green black 44.0 male masculine Rodia Rodian A New Hope
Jabba Desilijic Tiure 175 1358.0 NA green-tan, brown orange 600.0 hermaphroditic masculine Nal Hutta Hutt A New Hope , Return of the Jedi, The Phantom Menace
Wedge Antilles 170 77.0 brown fair hazel 21.0 male masculine Corellia Human A New Hope , The Empire Strikes Back, Return of the Jedi Snowspeeder X-wing
Yoda 66 17.0 white green brown 896.0 male masculine NA Yoda’s species The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith
Palpatine 170 75.0 grey pale yellow 82.0 male masculine Naboo Human The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith
Boba Fett 183 78.2 black fair brown 31.5 male masculine Kamino Human The Empire Strikes Back, Return of the Jedi , Attack of the Clones Slave 1
IG-88 200 140.0 none metal red 15.0 none masculine NA Droid The Empire Strikes Back
Bossk 190 113.0 none green red 53.0 male masculine Trandosha Trandoshan The Empire Strikes Back
Lando Calrissian 177 79.0 black dark brown 31.0 male masculine Socorro Human The Empire Strikes Back, Return of the Jedi Millennium Falcon
Lobot 175 79.0 none light blue 37.0 male masculine Bespin Human The Empire Strikes Back
Ackbar 180 83.0 none brown mottle orange 41.0 male masculine Mon Cala Mon Calamari Return of the Jedi, The Force Awakens
Arvel Crynyd NA NA brown fair brown NA male masculine NA Human Return of the Jedi A-wing
Wicket Systri Warrick 88 20.0 brown brown brown 8.0 male masculine Endor Ewok Return of the Jedi
Nien Nunb 160 68.0 none grey black NA male masculine Sullust Sullustan Return of the Jedi Millennium Falcon
Qui-Gon Jinn 193 89.0 brown fair blue 92.0 male masculine NA Human The Phantom Menace Tribubble bongo
Nute Gunray 191 90.0 none mottled green red NA male masculine Cato Neimoidia Neimodian The Phantom Menace , Attack of the Clones, Revenge of the Sith
Finis Valorum 170 NA blond fair blue 91.0 male masculine Coruscant Human The Phantom Menace
Jar Jar Binks 196 66.0 none orange orange 52.0 male masculine Naboo Gungan The Phantom Menace , Attack of the Clones
Roos Tarpals 224 82.0 none grey orange NA male masculine Naboo Gungan The Phantom Menace
Rugor Nass 206 NA none green orange NA male masculine Naboo Gungan The Phantom Menace
Ric Olié 183 NA brown fair blue NA male masculine Naboo Human The Phantom Menace Naboo Royal Starship
Watto 137 NA black blue, grey yellow NA male masculine Toydaria Toydarian The Phantom Menace , Attack of the Clones
Sebulba 112 40.0 none grey, red orange NA male masculine Malastare Dug The Phantom Menace
Quarsh Panaka 183 NA black dark brown 62.0 male masculine Naboo Human The Phantom Menace
Darth Maul 175 80.0 none red yellow 54.0 male masculine Dathomir Zabrak The Phantom Menace Sith speeder Scimitar
Bib Fortuna 180 NA none pale pink NA male masculine Ryloth Twi’lek Return of the Jedi
Ratts Tyerel 79 15.0 none grey, blue unknown NA male masculine Aleen Minor Aleena The Phantom Menace
Dud Bolt 94 45.0 none blue, grey yellow NA male masculine Vulpter Vulptereen The Phantom Menace
Gasgano 122 NA none white, blue black NA male masculine Troiken Xexto The Phantom Menace
Ben Quadinaros 163 65.0 none grey, green, yellow orange NA male masculine Tund Toong The Phantom Menace
Mace Windu 188 84.0 none dark brown 72.0 male masculine Haruun Kal Human The Phantom Menace , Attack of the Clones, Revenge of the Sith
Ki-Adi-Mundi 198 82.0 white pale yellow 92.0 male masculine Cerea Cerean The Phantom Menace , Attack of the Clones, Revenge of the Sith
Kit Fisto 196 87.0 none green black NA male masculine Glee Anselm Nautolan The Phantom Menace , Attack of the Clones, Revenge of the Sith
Eeth Koth 171 NA black brown brown NA male masculine Iridonia Zabrak The Phantom Menace , Revenge of the Sith
Saesee Tiin 188 NA none pale orange NA male masculine Iktotch Iktotchi The Phantom Menace , Revenge of the Sith
Yarael Poof 264 NA none white yellow NA male masculine Quermia Quermian The Phantom Menace
Plo Koon 188 80.0 none orange black 22.0 male masculine Dorin Kel Dor The Phantom Menace , Attack of the Clones, Revenge of the Sith Jedi starfighter
Mas Amedda 196 NA none blue blue NA male masculine Champala Chagrian The Phantom Menace , Attack of the Clones
Cliegg Lars 183 NA brown fair blue 82.0 male masculine Tatooine Human Attack of the Clones
Poggle the Lesser 183 80.0 none green yellow NA male masculine Geonosis Geonosian Attack of the Clones, Revenge of the Sith
Dooku 193 80.0 white fair brown 102.0 male masculine Serenno Human Attack of the Clones, Revenge of the Sith Flitknot speeder
Bail Prestor Organa 191 NA black tan brown 67.0 male masculine Alderaan Human Attack of the Clones, Revenge of the Sith
Jango Fett 183 79.0 black tan brown 66.0 male masculine Concord Dawn Human Attack of the Clones
Dexter Jettster 198 102.0 none brown yellow NA male masculine Ojom Besalisk Attack of the Clones
Lama Su 229 88.0 none grey black NA male masculine Kamino Kaminoan Attack of the Clones
Wat Tambor 193 48.0 none green, grey unknown NA male masculine Skako Skakoan Attack of the Clones
San Hill 191 NA none grey gold NA male masculine Muunilinst Muun Attack of the Clones
Grievous 216 159.0 none brown, white green, yellow NA male masculine Kalee Kaleesh Revenge of the Sith Tsmeu-6 personal wheel bike Belbullab-22 starfighter
Tarfful 234 136.0 brown brown blue NA male masculine Kashyyyk Wookiee Revenge of the Sith
Raymus Antilles 188 79.0 brown light brown NA male masculine Alderaan Human A New Hope , Revenge of the Sith
Tion Medon 206 80.0 none grey black NA male masculine Utapau Pau’an Revenge of the Sith
Finn NA NA black dark dark NA male masculine NA Human The Force Awakens
Poe Dameron NA NA brown light brown NA male masculine NA Human The Force Awakens X-wing
BB8 NA NA none none black NA none masculine NA Droid The Force Awakens

We can combine multiple conditions in the same call. Here, I’ve filtered out masculine characters, whose height is greater than the median height (i.e. they are in the top 50 percentile), and whose mass was not NA.

df.starwars %>% 
  filter(gender == "masculine",
         height > median(height, na.rm = T),
         !is.na(mass))
name height mass hair_color skin_color eye_color birth_year sex gender homeworld species films vehicles starships
Darth Vader 202 136.0 none white yellow 41.9 male masculine Tatooine Human A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith TIE Advanced x1
Biggs Darklighter 183 84.0 black light brown 24.0 male masculine Tatooine Human A New Hope X-wing
Obi-Wan Kenobi 182 77.0 auburn, white fair blue-gray 57.0 male masculine Stewjon Human A New Hope , The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith Tribubble bongo Jedi starfighter , Trade Federation cruiser, Naboo star skiff , Jedi Interceptor , Belbullab-22 starfighter
Anakin Skywalker 188 84.0 blond fair blue 41.9 male masculine Tatooine Human The Phantom Menace , Attack of the Clones, Revenge of the Sith Zephyr-G swoop bike, XJ-6 airspeeder Naboo fighter , Trade Federation cruiser, Jedi Interceptor
Chewbacca 228 112.0 brown unknown blue 200.0 male masculine Kashyyyk Wookiee A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith , The Force Awakens AT-ST Millennium Falcon, Imperial shuttle
Boba Fett 183 78.2 black fair brown 31.5 male masculine Kamino Human The Empire Strikes Back, Return of the Jedi , Attack of the Clones Slave 1
IG-88 200 140.0 none metal red 15.0 none masculine NA Droid The Empire Strikes Back
Bossk 190 113.0 none green red 53.0 male masculine Trandosha Trandoshan The Empire Strikes Back
Qui-Gon Jinn 193 89.0 brown fair blue 92.0 male masculine NA Human The Phantom Menace Tribubble bongo
Nute Gunray 191 90.0 none mottled green red NA male masculine Cato Neimoidia Neimodian The Phantom Menace , Attack of the Clones, Revenge of the Sith
Jar Jar Binks 196 66.0 none orange orange 52.0 male masculine Naboo Gungan The Phantom Menace , Attack of the Clones
Roos Tarpals 224 82.0 none grey orange NA male masculine Naboo Gungan The Phantom Menace
Mace Windu 188 84.0 none dark brown 72.0 male masculine Haruun Kal Human The Phantom Menace , Attack of the Clones, Revenge of the Sith
Ki-Adi-Mundi 198 82.0 white pale yellow 92.0 male masculine Cerea Cerean The Phantom Menace , Attack of the Clones, Revenge of the Sith
Kit Fisto 196 87.0 none green black NA male masculine Glee Anselm Nautolan The Phantom Menace , Attack of the Clones, Revenge of the Sith
Plo Koon 188 80.0 none orange black 22.0 male masculine Dorin Kel Dor The Phantom Menace , Attack of the Clones, Revenge of the Sith Jedi starfighter
Poggle the Lesser 183 80.0 none green yellow NA male masculine Geonosis Geonosian Attack of the Clones, Revenge of the Sith
Dooku 193 80.0 white fair brown 102.0 male masculine Serenno Human Attack of the Clones, Revenge of the Sith Flitknot speeder
Jango Fett 183 79.0 black tan brown 66.0 male masculine Concord Dawn Human Attack of the Clones
Dexter Jettster 198 102.0 none brown yellow NA male masculine Ojom Besalisk Attack of the Clones
Lama Su 229 88.0 none grey black NA male masculine Kamino Kaminoan Attack of the Clones
Wat Tambor 193 48.0 none green, grey unknown NA male masculine Skako Skakoan Attack of the Clones
Grievous 216 159.0 none brown, white green, yellow NA male masculine Kalee Kaleesh Revenge of the Sith Tsmeu-6 personal wheel bike Belbullab-22 starfighter
Tarfful 234 136.0 brown brown blue NA male masculine Kashyyyk Wookiee Revenge of the Sith
Raymus Antilles 188 79.0 brown light brown NA male masculine Alderaan Human A New Hope , Revenge of the Sith
Tion Medon 206 80.0 none grey black NA male masculine Utapau Pau’an Revenge of the Sith

Many functions like mean(), median(), var(), sd(), sum() have the argument na.rm which is set to FALSE by default. I set the argument to TRUE here (or T for short), which means that the NA values are ignored, and the median() is calculated based on the remaining values.

You can use , and & interchangeably in filter(). Make sure to use parentheses when combining several logical operators to indicate which logical operation should be performed first:

df.starwars %>% 
  filter((skin_color %in% c("dark", "pale") | sex == "hermaphroditic") & height > 170)
name height mass hair_color skin_color eye_color birth_year sex gender homeworld species films vehicles starships
Jabba Desilijic Tiure 175 1358 NA green-tan, brown orange 600 hermaphroditic masculine Nal Hutta Hutt A New Hope , Return of the Jedi, The Phantom Menace
Lando Calrissian 177 79 black dark brown 31 male masculine Socorro Human The Empire Strikes Back, Return of the Jedi Millennium Falcon
Quarsh Panaka 183 NA black dark brown 62 male masculine Naboo Human The Phantom Menace
Bib Fortuna 180 NA none pale pink NA male masculine Ryloth Twi’lek Return of the Jedi
Mace Windu 188 84 none dark brown 72 male masculine Haruun Kal Human The Phantom Menace , Attack of the Clones, Revenge of the Sith
Ki-Adi-Mundi 198 82 white pale yellow 92 male masculine Cerea Cerean The Phantom Menace , Attack of the Clones, Revenge of the Sith
Adi Gallia 184 50 none dark blue NA female feminine Coruscant Tholothian The Phantom Menace , Revenge of the Sith
Saesee Tiin 188 NA none pale orange NA male masculine Iktotch Iktotchi The Phantom Menace , Revenge of the Sith
Gregar Typho 185 85 black dark brown NA NA NA Naboo NA Attack of the Clones Naboo fighter
Sly Moore 178 48 none pale white NA NA NA Umbara NA Attack of the Clones, Revenge of the Sith

The starwars characters that have either a "dark" or a "pale" skin tone, or whose sex is "hermaphroditic", and whose height is at least 170 cm. The %in% operator is useful when there are multiple options. Instead of skin_color %in% c("dark", "pale"), I could have also written skin_color == "dark" | skin_color == "pale" but this gets cumbersome as the number of options increases.

arrange()

arrange() allows us to sort the values in a data frame by one or more column entries.

df.starwars %>% 
  arrange(hair_color, desc(height))
name height mass hair_color skin_color eye_color birth_year sex gender homeworld species films vehicles starships
Mon Mothma 150 NA auburn fair blue 48.0 female feminine Chandrila Human Return of the Jedi
Wilhuff Tarkin 180 NA auburn, grey fair blue 64.0 male masculine Eriadu Human A New Hope , Revenge of the Sith
Obi-Wan Kenobi 182 77.0 auburn, white fair blue-gray 57.0 male masculine Stewjon Human A New Hope , The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith Tribubble bongo Jedi starfighter , Trade Federation cruiser, Naboo star skiff , Jedi Interceptor , Belbullab-22 starfighter
Bail Prestor Organa 191 NA black tan brown 67.0 male masculine Alderaan Human Attack of the Clones, Revenge of the Sith
Gregar Typho 185 85.0 black dark brown NA NA NA Naboo NA Attack of the Clones Naboo fighter
Biggs Darklighter 183 84.0 black light brown 24.0 male masculine Tatooine Human A New Hope X-wing
Boba Fett 183 78.2 black fair brown 31.5 male masculine Kamino Human The Empire Strikes Back, Return of the Jedi , Attack of the Clones Slave 1
Quarsh Panaka 183 NA black dark brown 62.0 male masculine Naboo Human The Phantom Menace
Jango Fett 183 79.0 black tan brown 66.0 male masculine Concord Dawn Human Attack of the Clones
Lando Calrissian 177 79.0 black dark brown 31.0 male masculine Socorro Human The Empire Strikes Back, Return of the Jedi Millennium Falcon
Eeth Koth 171 NA black brown brown NA male masculine Iridonia Zabrak The Phantom Menace , Revenge of the Sith
Luminara Unduli 170 56.2 black yellow blue 58.0 female feminine Mirial Mirialan Attack of the Clones, Revenge of the Sith
Barriss Offee 166 50.0 black yellow blue 40.0 female feminine Mirial Mirialan Attack of the Clones
Shmi Skywalker 163 NA black fair brown 72.0 female feminine Tatooine Human The Phantom Menace , Attack of the Clones
Watto 137 NA black blue, grey yellow NA male masculine Toydaria Toydarian The Phantom Menace , Attack of the Clones
Finn NA NA black dark dark NA male masculine NA Human The Force Awakens
Anakin Skywalker 188 84.0 blond fair blue 41.9 male masculine Tatooine Human The Phantom Menace , Attack of the Clones, Revenge of the Sith Zephyr-G swoop bike, XJ-6 airspeeder Naboo fighter , Trade Federation cruiser, Jedi Interceptor
Luke Skywalker 172 77.0 blond fair blue 19.0 male masculine Tatooine Human A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith , The Force Awakens Snowspeeder , Imperial Speeder Bike X-wing , Imperial shuttle
Finis Valorum 170 NA blond fair blue 91.0 male masculine Coruscant Human The Phantom Menace
Zam Wesell 168 55.0 blonde fair, green, yellow yellow NA female feminine Zolan Clawdite Attack of the Clones Koro-2 Exodrive airspeeder
Tarfful 234 136.0 brown brown blue NA male masculine Kashyyyk Wookiee Revenge of the Sith
Chewbacca 228 112.0 brown unknown blue 200.0 male masculine Kashyyyk Wookiee A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith , The Force Awakens AT-ST Millennium Falcon, Imperial shuttle
Qui-Gon Jinn 193 89.0 brown fair blue 92.0 male masculine NA Human The Phantom Menace Tribubble bongo
Raymus Antilles 188 79.0 brown light brown NA male masculine Alderaan Human A New Hope , Revenge of the Sith
Padmé Amidala 185 45.0 brown light brown 46.0 female feminine Naboo Human The Phantom Menace , Attack of the Clones, Revenge of the Sith Naboo fighter , H-type Nubian yacht, Naboo star skiff
Ric Olié 183 NA brown fair blue NA male masculine Naboo Human The Phantom Menace Naboo Royal Starship
Cliegg Lars 183 NA brown fair blue 82.0 male masculine Tatooine Human Attack of the Clones
Han Solo 180 80.0 brown fair brown 29.0 male masculine Corellia Human A New Hope , The Empire Strikes Back, Return of the Jedi , The Force Awakens Millennium Falcon, Imperial shuttle
Jek Tono Porkins 180 110.0 brown fair blue NA NA NA Bestine IV NA A New Hope X-wing
Wedge Antilles 170 77.0 brown fair hazel 21.0 male masculine Corellia Human A New Hope , The Empire Strikes Back, Return of the Jedi Snowspeeder X-wing
Beru Whitesun Lars 165 75.0 brown light blue 47.0 female feminine Tatooine Human A New Hope , Attack of the Clones, Revenge of the Sith
Dormé 165 NA brown light brown NA female feminine Naboo Human Attack of the Clones
Cordé 157 NA brown light brown NA NA NA Naboo NA Attack of the Clones
Leia Organa 150 49.0 brown light brown 19.0 female feminine Alderaan Human A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith , The Force Awakens Imperial Speeder Bike
Wicket Systri Warrick 88 20.0 brown brown brown 8.0 male masculine Endor Ewok Return of the Jedi
Arvel Crynyd NA NA brown fair brown NA male masculine NA Human Return of the Jedi A-wing
Rey NA NA brown light hazel NA female feminine NA Human The Force Awakens
Poe Dameron NA NA brown light brown NA male masculine NA Human The Force Awakens X-wing
Owen Lars 178 120.0 brown, grey light blue 52.0 male masculine Tatooine Human A New Hope , Attack of the Clones, Revenge of the Sith
Palpatine 170 75.0 grey pale yellow 82.0 male masculine Naboo Human The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith
Yarael Poof 264 NA none white yellow NA male masculine Quermia Quermian The Phantom Menace
Lama Su 229 88.0 none grey black NA male masculine Kamino Kaminoan Attack of the Clones
Roos Tarpals 224 82.0 none grey orange NA male masculine Naboo Gungan The Phantom Menace
Grievous 216 159.0 none brown, white green, yellow NA male masculine Kalee Kaleesh Revenge of the Sith Tsmeu-6 personal wheel bike Belbullab-22 starfighter
Taun We 213 NA none grey black NA female feminine Kamino Kaminoan Attack of the Clones
Rugor Nass 206 NA none green orange NA male masculine Naboo Gungan The Phantom Menace
Tion Medon 206 80.0 none grey black NA male masculine Utapau Pau’an Revenge of the Sith
Darth Vader 202 136.0 none white yellow 41.9 male masculine Tatooine Human A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith TIE Advanced x1
IG-88 200 140.0 none metal red 15.0 none masculine NA Droid The Empire Strikes Back
Dexter Jettster 198 102.0 none brown yellow NA male masculine Ojom Besalisk Attack of the Clones
Jar Jar Binks 196 66.0 none orange orange 52.0 male masculine Naboo Gungan The Phantom Menace , Attack of the Clones
Kit Fisto 196 87.0 none green black NA male masculine Glee Anselm Nautolan The Phantom Menace , Attack of the Clones, Revenge of the Sith
Mas Amedda 196 NA none blue blue NA male masculine Champala Chagrian The Phantom Menace , Attack of the Clones
Wat Tambor 193 48.0 none green, grey unknown NA male masculine Skako Skakoan Attack of the Clones
Nute Gunray 191 90.0 none mottled green red NA male masculine Cato Neimoidia Neimodian The Phantom Menace , Attack of the Clones, Revenge of the Sith
San Hill 191 NA none grey gold NA male masculine Muunilinst Muun Attack of the Clones
Bossk 190 113.0 none green red 53.0 male masculine Trandosha Trandoshan The Empire Strikes Back
Mace Windu 188 84.0 none dark brown 72.0 male masculine Haruun Kal Human The Phantom Menace , Attack of the Clones, Revenge of the Sith
Saesee Tiin 188 NA none pale orange NA male masculine Iktotch Iktotchi The Phantom Menace , Revenge of the Sith
Plo Koon 188 80.0 none orange black 22.0 male masculine Dorin Kel Dor The Phantom Menace , Attack of the Clones, Revenge of the Sith Jedi starfighter
Adi Gallia 184 50.0 none dark blue NA female feminine Coruscant Tholothian The Phantom Menace , Revenge of the Sith
Poggle the Lesser 183 80.0 none green yellow NA male masculine Geonosis Geonosian Attack of the Clones, Revenge of the Sith
Ackbar 180 83.0 none brown mottle orange 41.0 male masculine Mon Cala Mon Calamari Return of the Jedi, The Force Awakens
Bib Fortuna 180 NA none pale pink NA male masculine Ryloth Twi’lek Return of the Jedi
Ayla Secura 178 55.0 none blue hazel 48.0 female feminine Ryloth Twi’lek The Phantom Menace , Attack of the Clones, Revenge of the Sith
Shaak Ti 178 57.0 none red, blue, white black NA female feminine Shili Togruta Attack of the Clones, Revenge of the Sith
Sly Moore 178 48.0 none pale white NA NA NA Umbara NA Attack of the Clones, Revenge of the Sith
Lobot 175 79.0 none light blue 37.0 male masculine Bespin Human The Empire Strikes Back
Darth Maul 175 80.0 none red yellow 54.0 male masculine Dathomir Zabrak The Phantom Menace Sith speeder Scimitar
Ben Quadinaros 163 65.0 none grey, green, yellow orange NA male masculine Tund Toong The Phantom Menace
Nien Nunb 160 68.0 none grey black NA male masculine Sullust Sullustan Return of the Jedi Millennium Falcon
Gasgano 122 NA none white, blue black NA male masculine Troiken Xexto The Phantom Menace
Sebulba 112 40.0 none grey, red orange NA male masculine Malastare Dug The Phantom Menace
R4-P17 96 NA none silver, red red, blue NA none feminine NA Droid Attack of the Clones, Revenge of the Sith
Dud Bolt 94 45.0 none blue, grey yellow NA male masculine Vulpter Vulptereen The Phantom Menace
Ratts Tyerel 79 15.0 none grey, blue unknown NA male masculine Aleen Minor Aleena The Phantom Menace
BB8 NA NA none none black NA none masculine NA Droid The Force Awakens
Captain Phasma NA NA none none unknown NA female feminine NA Human The Force Awakens
Ki-Adi-Mundi 198 82.0 white pale yellow 92.0 male masculine Cerea Cerean The Phantom Menace , Attack of the Clones, Revenge of the Sith
Dooku 193 80.0 white fair brown 102.0 male masculine Serenno Human Attack of the Clones, Revenge of the Sith Flitknot speeder
Jocasta Nu 167 NA white fair blue NA female feminine Coruscant Human Attack of the Clones
Yoda 66 17.0 white green brown 896.0 male masculine NA Yoda’s species The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith
Jabba Desilijic Tiure 175 1358.0 NA green-tan, brown orange 600.0 hermaphroditic masculine Nal Hutta Hutt A New Hope , Return of the Jedi, The Phantom Menace
Greedo 173 74.0 NA green black 44.0 male masculine Rodia Rodian A New Hope
C-3PO 167 75.0 NA gold yellow 112.0 none masculine Tatooine Droid A New Hope , The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith
R5-D4 97 32.0 NA white, red red NA none masculine Tatooine Droid A New Hope
R2-D2 96 32.0 NA white, blue red 33.0 none masculine Naboo Droid A New Hope , The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith , The Force Awakens

Here, I’ve sorted the data frame first by hair_color, and then by height. I’ve used the desc() function to sort height in descending order. Bail Prestor Organa is the tallest black character in starwars.

rename()

rename() renames column names.

df.starwars %>% 
  rename(person = name,
         mass_kg = mass)
person height mass_kg hair_color skin_color eye_color birth_year sex gender homeworld species films vehicles starships
Luke Skywalker 172 77.0 blond fair blue 19.0 male masculine Tatooine Human A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith , The Force Awakens Snowspeeder , Imperial Speeder Bike X-wing , Imperial shuttle
C-3PO 167 75.0 NA gold yellow 112.0 none masculine Tatooine Droid A New Hope , The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith
R2-D2 96 32.0 NA white, blue red 33.0 none masculine Naboo Droid A New Hope , The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith , The Force Awakens
Darth Vader 202 136.0 none white yellow 41.9 male masculine Tatooine Human A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith TIE Advanced x1
Leia Organa 150 49.0 brown light brown 19.0 female feminine Alderaan Human A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith , The Force Awakens Imperial Speeder Bike
Owen Lars 178 120.0 brown, grey light blue 52.0 male masculine Tatooine Human A New Hope , Attack of the Clones, Revenge of the Sith
Beru Whitesun Lars 165 75.0 brown light blue 47.0 female feminine Tatooine Human A New Hope , Attack of the Clones, Revenge of the Sith
R5-D4 97 32.0 NA white, red red NA none masculine Tatooine Droid A New Hope
Biggs Darklighter 183 84.0 black light brown 24.0 male masculine Tatooine Human A New Hope X-wing
Obi-Wan Kenobi 182 77.0 auburn, white fair blue-gray 57.0 male masculine Stewjon Human A New Hope , The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith Tribubble bongo Jedi starfighter , Trade Federation cruiser, Naboo star skiff , Jedi Interceptor , Belbullab-22 starfighter
Anakin Skywalker 188 84.0 blond fair blue 41.9 male masculine Tatooine Human The Phantom Menace , Attack of the Clones, Revenge of the Sith Zephyr-G swoop bike, XJ-6 airspeeder Naboo fighter , Trade Federation cruiser, Jedi Interceptor
Wilhuff Tarkin 180 NA auburn, grey fair blue 64.0 male masculine Eriadu Human A New Hope , Revenge of the Sith
Chewbacca 228 112.0 brown unknown blue 200.0 male masculine Kashyyyk Wookiee A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith , The Force Awakens AT-ST Millennium Falcon, Imperial shuttle
Han Solo 180 80.0 brown fair brown 29.0 male masculine Corellia Human A New Hope , The Empire Strikes Back, Return of the Jedi , The Force Awakens Millennium Falcon, Imperial shuttle
Greedo 173 74.0 NA green black 44.0 male masculine Rodia Rodian A New Hope
Jabba Desilijic Tiure 175 1358.0 NA green-tan, brown orange 600.0 hermaphroditic masculine Nal Hutta Hutt A New Hope , Return of the Jedi, The Phantom Menace
Wedge Antilles 170 77.0 brown fair hazel 21.0 male masculine Corellia Human A New Hope , The Empire Strikes Back, Return of the Jedi Snowspeeder X-wing
Jek Tono Porkins 180 110.0 brown fair blue NA NA NA Bestine IV NA A New Hope X-wing
Yoda 66 17.0 white green brown 896.0 male masculine NA Yoda’s species The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith
Palpatine 170 75.0 grey pale yellow 82.0 male masculine Naboo Human The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith
Boba Fett 183 78.2 black fair brown 31.5 male masculine Kamino Human The Empire Strikes Back, Return of the Jedi , Attack of the Clones Slave 1
IG-88 200 140.0 none metal red 15.0 none masculine NA Droid The Empire Strikes Back
Bossk 190 113.0 none green red 53.0 male masculine Trandosha Trandoshan The Empire Strikes Back
Lando Calrissian 177 79.0 black dark brown 31.0 male masculine Socorro Human The Empire Strikes Back, Return of the Jedi Millennium Falcon
Lobot 175 79.0 none light blue 37.0 male masculine Bespin Human The Empire Strikes Back
Ackbar 180 83.0 none brown mottle orange 41.0 male masculine Mon Cala Mon Calamari Return of the Jedi, The Force Awakens
Mon Mothma 150 NA auburn fair blue 48.0 female feminine Chandrila Human Return of the Jedi
Arvel Crynyd NA NA brown fair brown NA male masculine NA Human Return of the Jedi A-wing
Wicket Systri Warrick 88 20.0 brown brown brown 8.0 male masculine Endor Ewok Return of the Jedi
Nien Nunb 160 68.0 none grey black NA male masculine Sullust Sullustan Return of the Jedi Millennium Falcon
Qui-Gon Jinn 193 89.0 brown fair blue 92.0 male masculine NA Human The Phantom Menace Tribubble bongo
Nute Gunray 191 90.0 none mottled green red NA male masculine Cato Neimoidia Neimodian The Phantom Menace , Attack of the Clones, Revenge of the Sith
Finis Valorum 170 NA blond fair blue 91.0 male masculine Coruscant Human The Phantom Menace
Padmé Amidala 185 45.0 brown light brown 46.0 female feminine Naboo Human The Phantom Menace , Attack of the Clones, Revenge of the Sith Naboo fighter , H-type Nubian yacht, Naboo star skiff
Jar Jar Binks 196 66.0 none orange orange 52.0 male masculine Naboo Gungan The Phantom Menace , Attack of the Clones
Roos Tarpals 224 82.0 none grey orange NA male masculine Naboo Gungan The Phantom Menace
Rugor Nass 206 NA none green orange NA male masculine Naboo Gungan The Phantom Menace
Ric Olié 183 NA brown fair blue NA male masculine Naboo Human The Phantom Menace Naboo Royal Starship
Watto 137 NA black blue, grey yellow NA male masculine Toydaria Toydarian The Phantom Menace , Attack of the Clones
Sebulba 112 40.0 none grey, red orange NA male masculine Malastare Dug The Phantom Menace
Quarsh Panaka 183 NA black dark brown 62.0 male masculine Naboo Human The Phantom Menace
Shmi Skywalker 163 NA black fair brown 72.0 female feminine Tatooine Human The Phantom Menace , Attack of the Clones
Darth Maul 175 80.0 none red yellow 54.0 male masculine Dathomir Zabrak The Phantom Menace Sith speeder Scimitar
Bib Fortuna 180 NA none pale pink NA male masculine Ryloth Twi’lek Return of the Jedi
Ayla Secura 178 55.0 none blue hazel 48.0 female feminine Ryloth Twi’lek The Phantom Menace , Attack of the Clones, Revenge of the Sith
Ratts Tyerel 79 15.0 none grey, blue unknown NA male masculine Aleen Minor Aleena The Phantom Menace
Dud Bolt 94 45.0 none blue, grey yellow NA male masculine Vulpter Vulptereen The Phantom Menace
Gasgano 122 NA none white, blue black NA male masculine Troiken Xexto The Phantom Menace
Ben Quadinaros 163 65.0 none grey, green, yellow orange NA male masculine Tund Toong The Phantom Menace
Mace Windu 188 84.0 none dark brown 72.0 male masculine Haruun Kal Human The Phantom Menace , Attack of the Clones, Revenge of the Sith
Ki-Adi-Mundi 198 82.0 white pale yellow 92.0 male masculine Cerea Cerean The Phantom Menace , Attack of the Clones, Revenge of the Sith
Kit Fisto 196 87.0 none green black NA male masculine Glee Anselm Nautolan The Phantom Menace , Attack of the Clones, Revenge of the Sith
Eeth Koth 171 NA black brown brown NA male masculine Iridonia Zabrak The Phantom Menace , Revenge of the Sith
Adi Gallia 184 50.0 none dark blue NA female feminine Coruscant Tholothian The Phantom Menace , Revenge of the Sith
Saesee Tiin 188 NA none pale orange NA male masculine Iktotch Iktotchi The Phantom Menace , Revenge of the Sith
Yarael Poof 264 NA none white yellow NA male masculine Quermia Quermian The Phantom Menace
Plo Koon 188 80.0 none orange black 22.0 male masculine Dorin Kel Dor The Phantom Menace , Attack of the Clones, Revenge of the Sith Jedi starfighter
Mas Amedda 196 NA none blue blue NA male masculine Champala Chagrian The Phantom Menace , Attack of the Clones
Gregar Typho 185 85.0 black dark brown NA NA NA Naboo NA Attack of the Clones Naboo fighter
Cordé 157 NA brown light brown NA NA NA Naboo NA Attack of the Clones
Cliegg Lars 183 NA brown fair blue 82.0 male masculine Tatooine Human Attack of the Clones
Poggle the Lesser 183 80.0 none green yellow NA male masculine Geonosis Geonosian Attack of the Clones, Revenge of the Sith
Luminara Unduli 170 56.2 black yellow blue 58.0 female feminine Mirial Mirialan Attack of the Clones, Revenge of the Sith
Barriss Offee 166 50.0 black yellow blue 40.0 female feminine Mirial Mirialan Attack of the Clones
Dormé 165 NA brown light brown NA female feminine Naboo Human Attack of the Clones
Dooku 193 80.0 white fair brown 102.0 male masculine Serenno Human Attack of the Clones, Revenge of the Sith Flitknot speeder
Bail Prestor Organa 191 NA black tan brown 67.0 male masculine Alderaan Human Attack of the Clones, Revenge of the Sith
Jango Fett 183 79.0 black tan brown 66.0 male masculine Concord Dawn Human Attack of the Clones
Zam Wesell 168 55.0 blonde fair, green, yellow yellow NA female feminine Zolan Clawdite Attack of the Clones Koro-2 Exodrive airspeeder
Dexter Jettster 198 102.0 none brown yellow NA male masculine Ojom Besalisk Attack of the Clones
Lama Su 229 88.0 none grey black NA male masculine Kamino Kaminoan Attack of the Clones
Taun We 213 NA none grey black NA female feminine Kamino Kaminoan Attack of the Clones
Jocasta Nu 167 NA white fair blue NA female feminine Coruscant Human Attack of the Clones
R4-P17 96 NA none silver, red red, blue NA none feminine NA Droid Attack of the Clones, Revenge of the Sith
Wat Tambor 193 48.0 none green, grey unknown NA male masculine Skako Skakoan Attack of the Clones
San Hill 191 NA none grey gold NA male masculine Muunilinst Muun Attack of the Clones
Shaak Ti 178 57.0 none red, blue, white black NA female feminine Shili Togruta Attack of the Clones, Revenge of the Sith
Grievous 216 159.0 none brown, white green, yellow NA male masculine Kalee Kaleesh Revenge of the Sith Tsmeu-6 personal wheel bike Belbullab-22 starfighter
Tarfful 234 136.0 brown brown blue NA male masculine Kashyyyk Wookiee Revenge of the Sith
Raymus Antilles 188 79.0 brown light brown NA male masculine Alderaan Human A New Hope , Revenge of the Sith
Sly Moore 178 48.0 none pale white NA NA NA Umbara NA Attack of the Clones, Revenge of the Sith
Tion Medon 206 80.0 none grey black NA male masculine Utapau Pau’an Revenge of the Sith
Finn NA NA black dark dark NA male masculine NA Human The Force Awakens
Rey NA NA brown light hazel NA female feminine NA Human The Force Awakens
Poe Dameron NA NA brown light brown NA male masculine NA Human The Force Awakens X-wing
BB8 NA NA none none black NA none masculine NA Droid The Force Awakens
Captain Phasma NA NA none none unknown NA female feminine NA Human The Force Awakens

The new variable names goes on the LHS of the= sign, and the old name on the RHS.

To rename all variables at the same time use rename_with():

df.starwars %>%
  rename_with(.fn = ~ toupper(.))
NAME HEIGHT MASS HAIR_COLOR SKIN_COLOR EYE_COLOR BIRTH_YEAR SEX GENDER HOMEWORLD SPECIES FILMS VEHICLES STARSHIPS
Luke Skywalker 172 77.0 blond fair blue 19.0 male masculine Tatooine Human A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith , The Force Awakens Snowspeeder , Imperial Speeder Bike X-wing , Imperial shuttle
C-3PO 167 75.0 NA gold yellow 112.0 none masculine Tatooine Droid A New Hope , The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith
R2-D2 96 32.0 NA white, blue red 33.0 none masculine Naboo Droid A New Hope , The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith , The Force Awakens
Darth Vader 202 136.0 none white yellow 41.9 male masculine Tatooine Human A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith TIE Advanced x1
Leia Organa 150 49.0 brown light brown 19.0 female feminine Alderaan Human A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith , The Force Awakens Imperial Speeder Bike
Owen Lars 178 120.0 brown, grey light blue 52.0 male masculine Tatooine Human A New Hope , Attack of the Clones, Revenge of the Sith
Beru Whitesun Lars 165 75.0 brown light blue 47.0 female feminine Tatooine Human A New Hope , Attack of the Clones, Revenge of the Sith
R5-D4 97 32.0 NA white, red red NA none masculine Tatooine Droid A New Hope
Biggs Darklighter 183 84.0 black light brown 24.0 male masculine Tatooine Human A New Hope X-wing
Obi-Wan Kenobi 182 77.0 auburn, white fair blue-gray 57.0 male masculine Stewjon Human A New Hope , The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith Tribubble bongo Jedi starfighter , Trade Federation cruiser, Naboo star skiff , Jedi Interceptor , Belbullab-22 starfighter
Anakin Skywalker 188 84.0 blond fair blue 41.9 male masculine Tatooine Human The Phantom Menace , Attack of the Clones, Revenge of the Sith Zephyr-G swoop bike, XJ-6 airspeeder Naboo fighter , Trade Federation cruiser, Jedi Interceptor
Wilhuff Tarkin 180 NA auburn, grey fair blue 64.0 male masculine Eriadu Human A New Hope , Revenge of the Sith
Chewbacca 228 112.0 brown unknown blue 200.0 male masculine Kashyyyk Wookiee A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith , The Force Awakens AT-ST Millennium Falcon, Imperial shuttle
Han Solo 180 80.0 brown fair brown 29.0 male masculine Corellia Human A New Hope , The Empire Strikes Back, Return of the Jedi , The Force Awakens Millennium Falcon, Imperial shuttle
Greedo 173 74.0 NA green black 44.0 male masculine Rodia Rodian A New Hope
Jabba Desilijic Tiure 175 1358.0 NA green-tan, brown orange 600.0 hermaphroditic masculine Nal Hutta Hutt A New Hope , Return of the Jedi, The Phantom Menace
Wedge Antilles 170 77.0 brown fair hazel 21.0 male masculine Corellia Human A New Hope , The Empire Strikes Back, Return of the Jedi Snowspeeder X-wing
Jek Tono Porkins 180 110.0 brown fair blue NA NA NA Bestine IV NA A New Hope X-wing
Yoda 66 17.0 white green brown 896.0 male masculine NA Yoda’s species The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith
Palpatine 170 75.0 grey pale yellow 82.0 male masculine Naboo Human The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith
Boba Fett 183 78.2 black fair brown 31.5 male masculine Kamino Human The Empire Strikes Back, Return of the Jedi , Attack of the Clones Slave 1
IG-88 200 140.0 none metal red 15.0 none masculine NA Droid The Empire Strikes Back
Bossk 190 113.0 none green red 53.0 male masculine Trandosha Trandoshan The Empire Strikes Back
Lando Calrissian 177 79.0 black dark brown 31.0 male masculine Socorro Human The Empire Strikes Back, Return of the Jedi Millennium Falcon
Lobot 175 79.0 none light blue 37.0 male masculine Bespin Human The Empire Strikes Back
Ackbar 180 83.0 none brown mottle orange 41.0 male masculine Mon Cala Mon Calamari Return of the Jedi, The Force Awakens
Mon Mothma 150 NA auburn fair blue 48.0 female feminine Chandrila Human Return of the Jedi
Arvel Crynyd NA NA brown fair brown NA male masculine NA Human Return of the Jedi A-wing
Wicket Systri Warrick 88 20.0 brown brown brown 8.0 male masculine Endor Ewok Return of the Jedi
Nien Nunb 160 68.0 none grey black NA male masculine Sullust Sullustan Return of the Jedi Millennium Falcon
Qui-Gon Jinn 193 89.0 brown fair blue 92.0 male masculine NA Human The Phantom Menace Tribubble bongo
Nute Gunray 191 90.0 none mottled green red NA male masculine Cato Neimoidia Neimodian The Phantom Menace , Attack of the Clones, Revenge of the Sith
Finis Valorum 170 NA blond fair blue 91.0 male masculine Coruscant Human The Phantom Menace
Padmé Amidala 185 45.0 brown light brown 46.0 female feminine Naboo Human The Phantom Menace , Attack of the Clones, Revenge of the Sith Naboo fighter , H-type Nubian yacht, Naboo star skiff
Jar Jar Binks 196 66.0 none orange orange 52.0 male masculine Naboo Gungan The Phantom Menace , Attack of the Clones
Roos Tarpals 224 82.0 none grey orange NA male masculine Naboo Gungan The Phantom Menace
Rugor Nass 206 NA none green orange NA male masculine Naboo Gungan The Phantom Menace
Ric Olié 183 NA brown fair blue NA male masculine Naboo Human The Phantom Menace Naboo Royal Starship
Watto 137 NA black blue, grey yellow NA male masculine Toydaria Toydarian The Phantom Menace , Attack of the Clones
Sebulba 112 40.0 none grey, red orange NA male masculine Malastare Dug The Phantom Menace
Quarsh Panaka 183 NA black dark brown 62.0 male masculine Naboo Human The Phantom Menace
Shmi Skywalker 163 NA black fair brown 72.0 female feminine Tatooine Human The Phantom Menace , Attack of the Clones
Darth Maul 175 80.0 none red yellow 54.0 male masculine Dathomir Zabrak The Phantom Menace Sith speeder Scimitar
Bib Fortuna 180 NA none pale pink NA male masculine Ryloth Twi’lek Return of the Jedi
Ayla Secura 178 55.0 none blue hazel 48.0 female feminine Ryloth Twi’lek The Phantom Menace , Attack of the Clones, Revenge of the Sith
Ratts Tyerel 79 15.0 none grey, blue unknown NA male masculine Aleen Minor Aleena The Phantom Menace
Dud Bolt 94 45.0 none blue, grey yellow NA male masculine Vulpter Vulptereen The Phantom Menace
Gasgano 122 NA none white, blue black NA male masculine Troiken Xexto The Phantom Menace
Ben Quadinaros 163 65.0 none grey, green, yellow orange NA male masculine Tund Toong The Phantom Menace
Mace Windu 188 84.0 none dark brown 72.0 male masculine Haruun Kal Human The Phantom Menace , Attack of the Clones, Revenge of the Sith
Ki-Adi-Mundi 198 82.0 white pale yellow 92.0 male masculine Cerea Cerean The Phantom Menace , Attack of the Clones, Revenge of the Sith
Kit Fisto 196 87.0 none green black NA male masculine Glee Anselm Nautolan The Phantom Menace , Attack of the Clones, Revenge of the Sith
Eeth Koth 171 NA black brown brown NA male masculine Iridonia Zabrak The Phantom Menace , Revenge of the Sith
Adi Gallia 184 50.0 none dark blue NA female feminine Coruscant Tholothian The Phantom Menace , Revenge of the Sith
Saesee Tiin 188 NA none pale orange NA male masculine Iktotch Iktotchi The Phantom Menace , Revenge of the Sith
Yarael Poof 264 NA none white yellow NA male masculine Quermia Quermian The Phantom Menace
Plo Koon 188 80.0 none orange black 22.0 male masculine Dorin Kel Dor The Phantom Menace , Attack of the Clones, Revenge of the Sith Jedi starfighter
Mas Amedda 196 NA none blue blue NA male masculine Champala Chagrian The Phantom Menace , Attack of the Clones
Gregar Typho 185 85.0 black dark brown NA NA NA Naboo NA Attack of the Clones Naboo fighter
Cordé 157 NA brown light brown NA NA NA Naboo NA Attack of the Clones
Cliegg Lars 183 NA brown fair blue 82.0 male masculine Tatooine Human Attack of the Clones
Poggle the Lesser 183 80.0 none green yellow NA male masculine Geonosis Geonosian Attack of the Clones, Revenge of the Sith
Luminara Unduli 170 56.2 black yellow blue 58.0 female feminine Mirial Mirialan Attack of the Clones, Revenge of the Sith
Barriss Offee 166 50.0 black yellow blue 40.0 female feminine Mirial Mirialan Attack of the Clones
Dormé 165 NA brown light brown NA female feminine Naboo Human Attack of the Clones
Dooku 193 80.0 white fair brown 102.0 male masculine Serenno Human Attack of the Clones, Revenge of the Sith Flitknot speeder
Bail Prestor Organa 191 NA black tan brown 67.0 male masculine Alderaan Human Attack of the Clones, Revenge of the Sith
Jango Fett 183 79.0 black tan brown 66.0 male masculine Concord Dawn Human Attack of the Clones
Zam Wesell 168 55.0 blonde fair, green, yellow yellow NA female feminine Zolan Clawdite Attack of the Clones Koro-2 Exodrive airspeeder
Dexter Jettster 198 102.0 none brown yellow NA male masculine Ojom Besalisk Attack of the Clones
Lama Su 229 88.0 none grey black NA male masculine Kamino Kaminoan Attack of the Clones
Taun We 213 NA none grey black NA female feminine Kamino Kaminoan Attack of the Clones
Jocasta Nu 167 NA white fair blue NA female feminine Coruscant Human Attack of the Clones
R4-P17 96 NA none silver, red red, blue NA none feminine NA Droid Attack of the Clones, Revenge of the Sith
Wat Tambor 193 48.0 none green, grey unknown NA male masculine Skako Skakoan Attack of the Clones
San Hill 191 NA none grey gold NA male masculine Muunilinst Muun Attack of the Clones
Shaak Ti 178 57.0 none red, blue, white black NA female feminine Shili Togruta Attack of the Clones, Revenge of the Sith
Grievous 216 159.0 none brown, white green, yellow NA male masculine Kalee Kaleesh Revenge of the Sith Tsmeu-6 personal wheel bike Belbullab-22 starfighter
Tarfful 234 136.0 brown brown blue NA male masculine Kashyyyk Wookiee Revenge of the Sith
Raymus Antilles 188 79.0 brown light brown NA male masculine Alderaan Human A New Hope , Revenge of the Sith
Sly Moore 178 48.0 none pale white NA NA NA Umbara NA Attack of the Clones, Revenge of the Sith
Tion Medon 206 80.0 none grey black NA male masculine Utapau Pau’an Revenge of the Sith
Finn NA NA black dark dark NA male masculine NA Human The Force Awakens
Rey NA NA brown light hazel NA female feminine NA Human The Force Awakens
Poe Dameron NA NA brown light brown NA male masculine NA Human The Force Awakens X-wing
BB8 NA NA none none black NA none masculine NA Droid The Force Awakens
Captain Phasma NA NA none none unknown NA female feminine NA Human The Force Awakens

Notice that I used the ~ here in the function call. I will explain what this does shortly.

relocate()

relocate() moves columns. For example, the following piece of code moves the species column to the front of the data frame:

df.starwars %>% 
  relocate(species)
species name height mass hair_color skin_color eye_color birth_year sex gender homeworld films vehicles starships
Human Luke Skywalker 172 77.0 blond fair blue 19.0 male masculine Tatooine A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith , The Force Awakens Snowspeeder , Imperial Speeder Bike X-wing , Imperial shuttle
Droid C-3PO 167 75.0 NA gold yellow 112.0 none masculine Tatooine A New Hope , The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith
Droid R2-D2 96 32.0 NA white, blue red 33.0 none masculine Naboo A New Hope , The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith , The Force Awakens
Human Darth Vader 202 136.0 none white yellow 41.9 male masculine Tatooine A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith TIE Advanced x1
Human Leia Organa 150 49.0 brown light brown 19.0 female feminine Alderaan A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith , The Force Awakens Imperial Speeder Bike
Human Owen Lars 178 120.0 brown, grey light blue 52.0 male masculine Tatooine A New Hope , Attack of the Clones, Revenge of the Sith
Human Beru Whitesun Lars 165 75.0 brown light blue 47.0 female feminine Tatooine A New Hope , Attack of the Clones, Revenge of the Sith
Droid R5-D4 97 32.0 NA white, red red NA none masculine Tatooine A New Hope
Human Biggs Darklighter 183 84.0 black light brown 24.0 male masculine Tatooine A New Hope X-wing
Human Obi-Wan Kenobi 182 77.0 auburn, white fair blue-gray 57.0 male masculine Stewjon A New Hope , The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith Tribubble bongo Jedi starfighter , Trade Federation cruiser, Naboo star skiff , Jedi Interceptor , Belbullab-22 starfighter
Human Anakin Skywalker 188 84.0 blond fair blue 41.9 male masculine Tatooine The Phantom Menace , Attack of the Clones, Revenge of the Sith Zephyr-G swoop bike, XJ-6 airspeeder Naboo fighter , Trade Federation cruiser, Jedi Interceptor
Human Wilhuff Tarkin 180 NA auburn, grey fair blue 64.0 male masculine Eriadu A New Hope , Revenge of the Sith
Wookiee Chewbacca 228 112.0 brown unknown blue 200.0 male masculine Kashyyyk A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith , The Force Awakens AT-ST Millennium Falcon, Imperial shuttle
Human Han Solo 180 80.0 brown fair brown 29.0 male masculine Corellia A New Hope , The Empire Strikes Back, Return of the Jedi , The Force Awakens Millennium Falcon, Imperial shuttle
Rodian Greedo 173 74.0 NA green black 44.0 male masculine Rodia A New Hope
Hutt Jabba Desilijic Tiure 175 1358.0 NA green-tan, brown orange 600.0 hermaphroditic masculine Nal Hutta A New Hope , Return of the Jedi, The Phantom Menace
Human Wedge Antilles 170 77.0 brown fair hazel 21.0 male masculine Corellia A New Hope , The Empire Strikes Back, Return of the Jedi Snowspeeder X-wing
NA Jek Tono Porkins 180 110.0 brown fair blue NA NA NA Bestine IV A New Hope X-wing
Yoda’s species Yoda 66 17.0 white green brown 896.0 male masculine NA The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith
Human Palpatine 170 75.0 grey pale yellow 82.0 male masculine Naboo The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith
Human Boba Fett 183 78.2 black fair brown 31.5 male masculine Kamino The Empire Strikes Back, Return of the Jedi , Attack of the Clones Slave 1
Droid IG-88 200 140.0 none metal red 15.0 none masculine NA The Empire Strikes Back
Trandoshan Bossk 190 113.0 none green red 53.0 male masculine Trandosha The Empire Strikes Back
Human Lando Calrissian 177 79.0 black dark brown 31.0 male masculine Socorro The Empire Strikes Back, Return of the Jedi Millennium Falcon
Human Lobot 175 79.0 none light blue 37.0 male masculine Bespin The Empire Strikes Back
Mon Calamari Ackbar 180 83.0 none brown mottle orange 41.0 male masculine Mon Cala Return of the Jedi, The Force Awakens
Human Mon Mothma 150 NA auburn fair blue 48.0 female feminine Chandrila Return of the Jedi
Human Arvel Crynyd NA NA brown fair brown NA male masculine NA Return of the Jedi A-wing
Ewok Wicket Systri Warrick 88 20.0 brown brown brown 8.0 male masculine Endor Return of the Jedi
Sullustan Nien Nunb 160 68.0 none grey black NA male masculine Sullust Return of the Jedi Millennium Falcon
Human Qui-Gon Jinn 193 89.0 brown fair blue 92.0 male masculine NA The Phantom Menace Tribubble bongo
Neimodian Nute Gunray 191 90.0 none mottled green red NA male masculine Cato Neimoidia The Phantom Menace , Attack of the Clones, Revenge of the Sith
Human Finis Valorum 170 NA blond fair blue 91.0 male masculine Coruscant The Phantom Menace
Human Padmé Amidala 185 45.0 brown light brown 46.0 female feminine Naboo The Phantom Menace , Attack of the Clones, Revenge of the Sith Naboo fighter , H-type Nubian yacht, Naboo star skiff
Gungan Jar Jar Binks 196 66.0 none orange orange 52.0 male masculine Naboo The Phantom Menace , Attack of the Clones
Gungan Roos Tarpals 224 82.0 none grey orange NA male masculine Naboo The Phantom Menace
Gungan Rugor Nass 206 NA none green orange NA male masculine Naboo The Phantom Menace
Human Ric Olié 183 NA brown fair blue NA male masculine Naboo The Phantom Menace Naboo Royal Starship
Toydarian Watto 137 NA black blue, grey yellow NA male masculine Toydaria The Phantom Menace , Attack of the Clones
Dug Sebulba 112 40.0 none grey, red orange NA male masculine Malastare The Phantom Menace
Human Quarsh Panaka 183 NA black dark brown 62.0 male masculine Naboo The Phantom Menace
Human Shmi Skywalker 163 NA black fair brown 72.0 female feminine Tatooine The Phantom Menace , Attack of the Clones
Zabrak Darth Maul 175 80.0 none red yellow 54.0 male masculine Dathomir The Phantom Menace Sith speeder Scimitar
Twi’lek Bib Fortuna 180 NA none pale pink NA male masculine Ryloth Return of the Jedi
Twi’lek Ayla Secura 178 55.0 none blue hazel 48.0 female feminine Ryloth The Phantom Menace , Attack of the Clones, Revenge of the Sith
Aleena Ratts Tyerel 79 15.0 none grey, blue unknown NA male masculine Aleen Minor The Phantom Menace
Vulptereen Dud Bolt 94 45.0 none blue, grey yellow NA male masculine Vulpter The Phantom Menace
Xexto Gasgano 122 NA none white, blue black NA male masculine Troiken The Phantom Menace
Toong Ben Quadinaros 163 65.0 none grey, green, yellow orange NA male masculine Tund The Phantom Menace
Human Mace Windu 188 84.0 none dark brown 72.0 male masculine Haruun Kal The Phantom Menace , Attack of the Clones, Revenge of the Sith
Cerean Ki-Adi-Mundi 198 82.0 white pale yellow 92.0 male masculine Cerea The Phantom Menace , Attack of the Clones, Revenge of the Sith
Nautolan Kit Fisto 196 87.0 none green black NA male masculine Glee Anselm The Phantom Menace , Attack of the Clones, Revenge of the Sith
Zabrak Eeth Koth 171 NA black brown brown NA male masculine Iridonia The Phantom Menace , Revenge of the Sith
Tholothian Adi Gallia 184 50.0 none dark blue NA female feminine Coruscant The Phantom Menace , Revenge of the Sith
Iktotchi Saesee Tiin 188 NA none pale orange NA male masculine Iktotch The Phantom Menace , Revenge of the Sith
Quermian Yarael Poof 264 NA none white yellow NA male masculine Quermia The Phantom Menace
Kel Dor Plo Koon 188 80.0 none orange black 22.0 male masculine Dorin The Phantom Menace , Attack of the Clones, Revenge of the Sith Jedi starfighter
Chagrian Mas Amedda 196 NA none blue blue NA male masculine Champala The Phantom Menace , Attack of the Clones
NA Gregar Typho 185 85.0 black dark brown NA NA NA Naboo Attack of the Clones Naboo fighter
NA Cordé 157 NA brown light brown NA NA NA Naboo Attack of the Clones
Human Cliegg Lars 183 NA brown fair blue 82.0 male masculine Tatooine Attack of the Clones
Geonosian Poggle the Lesser 183 80.0 none green yellow NA male masculine Geonosis Attack of the Clones, Revenge of the Sith
Mirialan Luminara Unduli 170 56.2 black yellow blue 58.0 female feminine Mirial Attack of the Clones, Revenge of the Sith
Mirialan Barriss Offee 166 50.0 black yellow blue 40.0 female feminine Mirial Attack of the Clones
Human Dormé 165 NA brown light brown NA female feminine Naboo Attack of the Clones
Human Dooku 193 80.0 white fair brown 102.0 male masculine Serenno Attack of the Clones, Revenge of the Sith Flitknot speeder
Human Bail Prestor Organa 191 NA black tan brown 67.0 male masculine Alderaan Attack of the Clones, Revenge of the Sith
Human Jango Fett 183 79.0 black tan brown 66.0 male masculine Concord Dawn Attack of the Clones
Clawdite Zam Wesell 168 55.0 blonde fair, green, yellow yellow NA female feminine Zolan Attack of the Clones Koro-2 Exodrive airspeeder
Besalisk Dexter Jettster 198 102.0 none brown yellow NA male masculine Ojom Attack of the Clones
Kaminoan Lama Su 229 88.0 none grey black NA male masculine Kamino Attack of the Clones
Kaminoan Taun We 213 NA none grey black NA female feminine Kamino Attack of the Clones
Human Jocasta Nu 167 NA white fair blue NA female feminine Coruscant Attack of the Clones
Droid R4-P17 96 NA none silver, red red, blue NA none feminine NA Attack of the Clones, Revenge of the Sith
Skakoan Wat Tambor 193 48.0 none green, grey unknown NA male masculine Skako Attack of the Clones
Muun San Hill 191 NA none grey gold NA male masculine Muunilinst Attack of the Clones
Togruta Shaak Ti 178 57.0 none red, blue, white black NA female feminine Shili Attack of the Clones, Revenge of the Sith
Kaleesh Grievous 216 159.0 none brown, white green, yellow NA male masculine Kalee Revenge of the Sith Tsmeu-6 personal wheel bike Belbullab-22 starfighter
Wookiee Tarfful 234 136.0 brown brown blue NA male masculine Kashyyyk Revenge of the Sith
Human Raymus Antilles 188 79.0 brown light brown NA male masculine Alderaan A New Hope , Revenge of the Sith
NA Sly Moore 178 48.0 none pale white NA NA NA Umbara Attack of the Clones, Revenge of the Sith
Pau’an Tion Medon 206 80.0 none grey black NA male masculine Utapau Revenge of the Sith
Human Finn NA NA black dark dark NA male masculine NA The Force Awakens
Human Rey NA NA brown light hazel NA female feminine NA The Force Awakens
Human Poe Dameron NA NA brown light brown NA male masculine NA The Force Awakens X-wing
Droid BB8 NA NA none none black NA none masculine NA The Force Awakens
Human Captain Phasma NA NA none none unknown NA female feminine NA The Force Awakens

We could also move the species column after the name column like so:

df.starwars %>% 
  relocate(species, .after = name)
name species height mass hair_color skin_color eye_color birth_year sex gender homeworld films vehicles starships
Luke Skywalker Human 172 77.0 blond fair blue 19.0 male masculine Tatooine A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith , The Force Awakens Snowspeeder , Imperial Speeder Bike X-wing , Imperial shuttle
C-3PO Droid 167 75.0 NA gold yellow 112.0 none masculine Tatooine A New Hope , The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith
R2-D2 Droid 96 32.0 NA white, blue red 33.0 none masculine Naboo A New Hope , The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith , The Force Awakens
Darth Vader Human 202 136.0 none white yellow 41.9 male masculine Tatooine A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith TIE Advanced x1
Leia Organa Human 150 49.0 brown light brown 19.0 female feminine Alderaan A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith , The Force Awakens Imperial Speeder Bike
Owen Lars Human 178 120.0 brown, grey light blue 52.0 male masculine Tatooine A New Hope , Attack of the Clones, Revenge of the Sith
Beru Whitesun Lars Human 165 75.0 brown light blue 47.0 female feminine Tatooine A New Hope , Attack of the Clones, Revenge of the Sith
R5-D4 Droid 97 32.0 NA white, red red NA none masculine Tatooine A New Hope
Biggs Darklighter Human 183 84.0 black light brown 24.0 male masculine Tatooine A New Hope X-wing
Obi-Wan Kenobi Human 182 77.0 auburn, white fair blue-gray 57.0 male masculine Stewjon A New Hope , The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith Tribubble bongo Jedi starfighter , Trade Federation cruiser, Naboo star skiff , Jedi Interceptor , Belbullab-22 starfighter
Anakin Skywalker Human 188 84.0 blond fair blue 41.9 male masculine Tatooine The Phantom Menace , Attack of the Clones, Revenge of the Sith Zephyr-G swoop bike, XJ-6 airspeeder Naboo fighter , Trade Federation cruiser, Jedi Interceptor
Wilhuff Tarkin Human 180 NA auburn, grey fair blue 64.0 male masculine Eriadu A New Hope , Revenge of the Sith
Chewbacca Wookiee 228 112.0 brown unknown blue 200.0 male masculine Kashyyyk A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith , The Force Awakens AT-ST Millennium Falcon, Imperial shuttle
Han Solo Human 180 80.0 brown fair brown 29.0 male masculine Corellia A New Hope , The Empire Strikes Back, Return of the Jedi , The Force Awakens Millennium Falcon, Imperial shuttle
Greedo Rodian 173 74.0 NA green black 44.0 male masculine Rodia A New Hope
Jabba Desilijic Tiure Hutt 175 1358.0 NA green-tan, brown orange 600.0 hermaphroditic masculine Nal Hutta A New Hope , Return of the Jedi, The Phantom Menace
Wedge Antilles Human 170 77.0 brown fair hazel 21.0 male masculine Corellia A New Hope , The Empire Strikes Back, Return of the Jedi Snowspeeder X-wing
Jek Tono Porkins NA 180 110.0 brown fair blue NA NA NA Bestine IV A New Hope X-wing
Yoda Yoda’s species 66 17.0 white green brown 896.0 male masculine NA The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith
Palpatine Human 170 75.0 grey pale yellow 82.0 male masculine Naboo The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith
Boba Fett Human 183 78.2 black fair brown 31.5 male masculine Kamino The Empire Strikes Back, Return of the Jedi , Attack of the Clones Slave 1
IG-88 Droid 200 140.0 none metal red 15.0 none masculine NA The Empire Strikes Back
Bossk Trandoshan 190 113.0 none green red 53.0 male masculine Trandosha The Empire Strikes Back
Lando Calrissian Human 177 79.0 black dark brown 31.0 male masculine Socorro The Empire Strikes Back, Return of the Jedi Millennium Falcon
Lobot Human 175 79.0 none light blue 37.0 male masculine Bespin The Empire Strikes Back
Ackbar Mon Calamari 180 83.0 none brown mottle orange 41.0 male masculine Mon Cala Return of the Jedi, The Force Awakens
Mon Mothma Human 150 NA auburn fair blue 48.0 female feminine Chandrila Return of the Jedi
Arvel Crynyd Human NA NA brown fair brown NA male masculine NA Return of the Jedi A-wing
Wicket Systri Warrick Ewok 88 20.0 brown brown brown 8.0 male masculine Endor Return of the Jedi
Nien Nunb Sullustan 160 68.0 none grey black NA male masculine Sullust Return of the Jedi Millennium Falcon
Qui-Gon Jinn Human 193 89.0 brown fair blue 92.0 male masculine NA The Phantom Menace Tribubble bongo
Nute Gunray Neimodian 191 90.0 none mottled green red NA male masculine Cato Neimoidia The Phantom Menace , Attack of the Clones, Revenge of the Sith
Finis Valorum Human 170 NA blond fair blue 91.0 male masculine Coruscant The Phantom Menace
Padmé Amidala Human 185 45.0 brown light brown 46.0 female feminine Naboo The Phantom Menace , Attack of the Clones, Revenge of the Sith Naboo fighter , H-type Nubian yacht, Naboo star skiff
Jar Jar Binks Gungan 196 66.0 none orange orange 52.0 male masculine Naboo The Phantom Menace , Attack of the Clones
Roos Tarpals Gungan 224 82.0 none grey orange NA male masculine Naboo The Phantom Menace
Rugor Nass Gungan 206 NA none green orange NA male masculine Naboo The Phantom Menace
Ric Olié Human 183 NA brown fair blue NA male masculine Naboo The Phantom Menace Naboo Royal Starship
Watto Toydarian 137 NA black blue, grey yellow NA male masculine Toydaria The Phantom Menace , Attack of the Clones
Sebulba Dug 112 40.0 none grey, red orange NA male masculine Malastare The Phantom Menace
Quarsh Panaka Human 183 NA black dark brown 62.0 male masculine Naboo The Phantom Menace
Shmi Skywalker Human 163 NA black fair brown 72.0 female feminine Tatooine The Phantom Menace , Attack of the Clones
Darth Maul Zabrak 175 80.0 none red yellow 54.0 male masculine Dathomir The Phantom Menace Sith speeder Scimitar
Bib Fortuna Twi’lek 180 NA none pale pink NA male masculine Ryloth Return of the Jedi
Ayla Secura Twi’lek 178 55.0 none blue hazel 48.0 female feminine Ryloth The Phantom Menace , Attack of the Clones, Revenge of the Sith
Ratts Tyerel Aleena 79 15.0 none grey, blue unknown NA male masculine Aleen Minor The Phantom Menace
Dud Bolt Vulptereen 94 45.0 none blue, grey yellow NA male masculine Vulpter The Phantom Menace
Gasgano Xexto 122 NA none white, blue black NA male masculine Troiken The Phantom Menace
Ben Quadinaros Toong 163 65.0 none grey, green, yellow orange NA male masculine Tund The Phantom Menace
Mace Windu Human 188 84.0 none dark brown 72.0 male masculine Haruun Kal The Phantom Menace , Attack of the Clones, Revenge of the Sith
Ki-Adi-Mundi Cerean 198 82.0 white pale yellow 92.0 male masculine Cerea The Phantom Menace , Attack of the Clones, Revenge of the Sith
Kit Fisto Nautolan 196 87.0 none green black NA male masculine Glee Anselm The Phantom Menace , Attack of the Clones, Revenge of the Sith
Eeth Koth Zabrak 171 NA black brown brown NA male masculine Iridonia The Phantom Menace , Revenge of the Sith
Adi Gallia Tholothian 184 50.0 none dark blue NA female feminine Coruscant The Phantom Menace , Revenge of the Sith
Saesee Tiin Iktotchi 188 NA none pale orange NA male masculine Iktotch The Phantom Menace , Revenge of the Sith
Yarael Poof Quermian 264 NA none white yellow NA male masculine Quermia The Phantom Menace
Plo Koon Kel Dor 188 80.0 none orange black 22.0 male masculine Dorin The Phantom Menace , Attack of the Clones, Revenge of the Sith Jedi starfighter
Mas Amedda Chagrian 196 NA none blue blue NA male masculine Champala The Phantom Menace , Attack of the Clones
Gregar Typho NA 185 85.0 black dark brown NA NA NA Naboo Attack of the Clones Naboo fighter
Cordé NA 157 NA brown light brown NA NA NA Naboo Attack of the Clones
Cliegg Lars Human 183 NA brown fair blue 82.0 male masculine Tatooine Attack of the Clones
Poggle the Lesser Geonosian 183 80.0 none green yellow NA male masculine Geonosis Attack of the Clones, Revenge of the Sith
Luminara Unduli Mirialan 170 56.2 black yellow blue 58.0 female feminine Mirial Attack of the Clones, Revenge of the Sith
Barriss Offee Mirialan 166 50.0 black yellow blue 40.0 female feminine Mirial Attack of the Clones
Dormé Human 165 NA brown light brown NA female feminine Naboo Attack of the Clones
Dooku Human 193 80.0 white fair brown 102.0 male masculine Serenno Attack of the Clones, Revenge of the Sith Flitknot speeder
Bail Prestor Organa Human 191 NA black tan brown 67.0 male masculine Alderaan Attack of the Clones, Revenge of the Sith
Jango Fett Human 183 79.0 black tan brown 66.0 male masculine Concord Dawn Attack of the Clones
Zam Wesell Clawdite 168 55.0 blonde fair, green, yellow yellow NA female feminine Zolan Attack of the Clones Koro-2 Exodrive airspeeder
Dexter Jettster Besalisk 198 102.0 none brown yellow NA male masculine Ojom Attack of the Clones
Lama Su Kaminoan 229 88.0 none grey black NA male masculine Kamino Attack of the Clones
Taun We Kaminoan 213 NA none grey black NA female feminine Kamino Attack of the Clones
Jocasta Nu Human 167 NA white fair blue NA female feminine Coruscant Attack of the Clones
R4-P17 Droid 96 NA none silver, red red, blue NA none feminine NA Attack of the Clones, Revenge of the Sith
Wat Tambor Skakoan 193 48.0 none green, grey unknown NA male masculine Skako Attack of the Clones
San Hill Muun 191 NA none grey gold NA male masculine Muunilinst Attack of the Clones
Shaak Ti Togruta 178 57.0 none red, blue, white black NA female feminine Shili Attack of the Clones, Revenge of the Sith
Grievous Kaleesh 216 159.0 none brown, white green, yellow NA male masculine Kalee Revenge of the Sith Tsmeu-6 personal wheel bike Belbullab-22 starfighter
Tarfful Wookiee 234 136.0 brown brown blue NA male masculine Kashyyyk Revenge of the Sith
Raymus Antilles Human 188 79.0 brown light brown NA male masculine Alderaan A New Hope , Revenge of the Sith
Sly Moore NA 178 48.0 none pale white NA NA NA Umbara Attack of the Clones, Revenge of the Sith
Tion Medon Pau’an 206 80.0 none grey black NA male masculine Utapau Revenge of the Sith
Finn Human NA NA black dark dark NA male masculine NA The Force Awakens
Rey Human NA NA brown light hazel NA female feminine NA The Force Awakens
Poe Dameron Human NA NA brown light brown NA male masculine NA The Force Awakens X-wing
BB8 Droid NA NA none none black NA none masculine NA The Force Awakens
Captain Phasma Human NA NA none none unknown NA female feminine NA The Force Awakens

select()

select() allows us to select a subset of the columns in the data frame.

df.starwars %>% 
  select(name, height, mass)
name height mass
Luke Skywalker 172 77.0
C-3PO 167 75.0
R2-D2 96 32.0
Darth Vader 202 136.0
Leia Organa 150 49.0
Owen Lars 178 120.0
Beru Whitesun Lars 165 75.0
R5-D4 97 32.0
Biggs Darklighter 183 84.0
Obi-Wan Kenobi 182 77.0
Anakin Skywalker 188 84.0
Wilhuff Tarkin 180 NA
Chewbacca 228 112.0
Han Solo 180 80.0
Greedo 173 74.0
Jabba Desilijic Tiure 175 1358.0
Wedge Antilles 170 77.0
Jek Tono Porkins 180 110.0
Yoda 66 17.0
Palpatine 170 75.0
Boba Fett 183 78.2
IG-88 200 140.0
Bossk 190 113.0
Lando Calrissian 177 79.0
Lobot 175 79.0
Ackbar 180 83.0
Mon Mothma 150 NA
Arvel Crynyd NA NA
Wicket Systri Warrick 88 20.0
Nien Nunb 160 68.0
Qui-Gon Jinn 193 89.0
Nute Gunray 191 90.0
Finis Valorum 170 NA
Padmé Amidala 185 45.0
Jar Jar Binks 196 66.0
Roos Tarpals 224 82.0
Rugor Nass 206 NA
Ric Olié 183 NA
Watto 137 NA
Sebulba 112 40.0
Quarsh Panaka 183 NA
Shmi Skywalker 163 NA
Darth Maul 175 80.0
Bib Fortuna 180 NA
Ayla Secura 178 55.0
Ratts Tyerel 79 15.0
Dud Bolt 94 45.0
Gasgano 122 NA
Ben Quadinaros 163 65.0
Mace Windu 188 84.0
Ki-Adi-Mundi 198 82.0
Kit Fisto 196 87.0
Eeth Koth 171 NA
Adi Gallia 184 50.0
Saesee Tiin 188 NA
Yarael Poof 264 NA
Plo Koon 188 80.0
Mas Amedda 196 NA
Gregar Typho 185 85.0
Cordé 157 NA
Cliegg Lars 183 NA
Poggle the Lesser 183 80.0
Luminara Unduli 170 56.2
Barriss Offee 166 50.0
Dormé 165 NA
Dooku 193 80.0
Bail Prestor Organa 191 NA
Jango Fett 183 79.0
Zam Wesell 168 55.0
Dexter Jettster 198 102.0
Lama Su 229 88.0
Taun We 213 NA
Jocasta Nu 167 NA
R4-P17 96 NA
Wat Tambor 193 48.0
San Hill 191 NA
Shaak Ti 178 57.0
Grievous 216 159.0
Tarfful 234 136.0
Raymus Antilles 188 79.0
Sly Moore 178 48.0
Tion Medon 206 80.0
Finn NA NA
Rey NA NA
Poe Dameron NA NA
BB8 NA NA
Captain Phasma NA NA

We can select multiple columns using the (from:to) syntax:

df.starwars %>%  
  select(name:birth_year) # from name to birth_year
name height mass hair_color skin_color eye_color birth_year
Luke Skywalker 172 77.0 blond fair blue 19.0
C-3PO 167 75.0 NA gold yellow 112.0
R2-D2 96 32.0 NA white, blue red 33.0
Darth Vader 202 136.0 none white yellow 41.9
Leia Organa 150 49.0 brown light brown 19.0
Owen Lars 178 120.0 brown, grey light blue 52.0
Beru Whitesun Lars 165 75.0 brown light blue 47.0
R5-D4 97 32.0 NA white, red red NA
Biggs Darklighter 183 84.0 black light brown 24.0
Obi-Wan Kenobi 182 77.0 auburn, white fair blue-gray 57.0
Anakin Skywalker 188 84.0 blond fair blue 41.9
Wilhuff Tarkin 180 NA auburn, grey fair blue 64.0
Chewbacca 228 112.0 brown unknown blue 200.0
Han Solo 180 80.0 brown fair brown 29.0
Greedo 173 74.0 NA green black 44.0
Jabba Desilijic Tiure 175 1358.0 NA green-tan, brown orange 600.0
Wedge Antilles 170 77.0 brown fair hazel 21.0
Jek Tono Porkins 180 110.0 brown fair blue NA
Yoda 66 17.0 white green brown 896.0
Palpatine 170 75.0 grey pale yellow 82.0
Boba Fett 183 78.2 black fair brown 31.5
IG-88 200 140.0 none metal red 15.0
Bossk 190 113.0 none green red 53.0
Lando Calrissian 177 79.0 black dark brown 31.0
Lobot 175 79.0 none light blue 37.0
Ackbar 180 83.0 none brown mottle orange 41.0
Mon Mothma 150 NA auburn fair blue 48.0
Arvel Crynyd NA NA brown fair brown NA
Wicket Systri Warrick 88 20.0 brown brown brown 8.0
Nien Nunb 160 68.0 none grey black NA
Qui-Gon Jinn 193 89.0 brown fair blue 92.0
Nute Gunray 191 90.0 none mottled green red NA
Finis Valorum 170 NA blond fair blue 91.0
Padmé Amidala 185 45.0 brown light brown 46.0
Jar Jar Binks 196 66.0 none orange orange 52.0
Roos Tarpals 224 82.0 none grey orange NA
Rugor Nass 206 NA none green orange NA
Ric Olié 183 NA brown fair blue NA
Watto 137 NA black blue, grey yellow NA
Sebulba 112 40.0 none grey, red orange NA
Quarsh Panaka 183 NA black dark brown 62.0
Shmi Skywalker 163 NA black fair brown 72.0
Darth Maul 175 80.0 none red yellow 54.0
Bib Fortuna 180 NA none pale pink NA
Ayla Secura 178 55.0 none blue hazel 48.0
Ratts Tyerel 79 15.0 none grey, blue unknown NA
Dud Bolt 94 45.0 none blue, grey yellow NA
Gasgano 122 NA none white, blue black NA
Ben Quadinaros 163 65.0 none grey, green, yellow orange NA
Mace Windu 188 84.0 none dark brown 72.0
Ki-Adi-Mundi 198 82.0 white pale yellow 92.0
Kit Fisto 196 87.0 none green black NA
Eeth Koth 171 NA black brown brown NA
Adi Gallia 184 50.0 none dark blue NA
Saesee Tiin 188 NA none pale orange NA
Yarael Poof 264 NA none white yellow NA
Plo Koon 188 80.0 none orange black 22.0
Mas Amedda 196 NA none blue blue NA
Gregar Typho 185 85.0 black dark brown NA
Cordé 157 NA brown light brown NA
Cliegg Lars 183 NA brown fair blue 82.0
Poggle the Lesser 183 80.0 none green yellow NA
Luminara Unduli 170 56.2 black yellow blue 58.0
Barriss Offee 166 50.0 black yellow blue 40.0
Dormé 165 NA brown light brown NA
Dooku 193 80.0 white fair brown 102.0
Bail Prestor Organa 191 NA black tan brown 67.0
Jango Fett 183 79.0 black tan brown 66.0
Zam Wesell 168 55.0 blonde fair, green, yellow yellow NA
Dexter Jettster 198 102.0 none brown yellow NA
Lama Su 229 88.0 none grey black NA
Taun We 213 NA none grey black NA
Jocasta Nu 167 NA white fair blue NA
R4-P17 96 NA none silver, red red, blue NA
Wat Tambor 193 48.0 none green, grey unknown NA
San Hill 191 NA none grey gold NA
Shaak Ti 178 57.0 none red, blue, white black NA
Grievous 216 159.0 none brown, white green, yellow NA
Tarfful 234 136.0 brown brown blue NA
Raymus Antilles 188 79.0 brown light brown NA
Sly Moore 178 48.0 none pale white NA
Tion Medon 206 80.0 none grey black NA
Finn NA NA black dark dark NA
Rey NA NA brown light hazel NA
Poe Dameron NA NA brown light brown NA
BB8 NA NA none none black NA
Captain Phasma NA NA none none unknown NA

Or use a variable for column selection:

columns = c("name", "height", "species")

df.starwars %>% 
  select(one_of(columns)) # useful when using a variable for column selection
name height species
Luke Skywalker 172 Human
C-3PO 167 Droid
R2-D2 96 Droid
Darth Vader 202 Human
Leia Organa 150 Human
Owen Lars 178 Human
Beru Whitesun Lars 165 Human
R5-D4 97 Droid
Biggs Darklighter 183 Human
Obi-Wan Kenobi 182 Human
Anakin Skywalker 188 Human
Wilhuff Tarkin 180 Human
Chewbacca 228 Wookiee
Han Solo 180 Human
Greedo 173 Rodian
Jabba Desilijic Tiure 175 Hutt
Wedge Antilles 170 Human
Jek Tono Porkins 180 NA
Yoda 66 Yoda’s species
Palpatine 170 Human
Boba Fett 183 Human
IG-88 200 Droid
Bossk 190 Trandoshan
Lando Calrissian 177 Human
Lobot 175 Human
Ackbar 180 Mon Calamari
Mon Mothma 150 Human
Arvel Crynyd NA Human
Wicket Systri Warrick 88 Ewok
Nien Nunb 160 Sullustan
Qui-Gon Jinn 193 Human
Nute Gunray 191 Neimodian
Finis Valorum 170 Human
Padmé Amidala 185 Human
Jar Jar Binks 196 Gungan
Roos Tarpals 224 Gungan
Rugor Nass 206 Gungan
Ric Olié 183 Human
Watto 137 Toydarian
Sebulba 112 Dug
Quarsh Panaka 183 Human
Shmi Skywalker 163 Human
Darth Maul 175 Zabrak
Bib Fortuna 180 Twi’lek
Ayla Secura 178 Twi’lek
Ratts Tyerel 79 Aleena
Dud Bolt 94 Vulptereen
Gasgano 122 Xexto
Ben Quadinaros 163 Toong
Mace Windu 188 Human
Ki-Adi-Mundi 198 Cerean
Kit Fisto 196 Nautolan
Eeth Koth 171 Zabrak
Adi Gallia 184 Tholothian
Saesee Tiin 188 Iktotchi
Yarael Poof 264 Quermian
Plo Koon 188 Kel Dor
Mas Amedda 196 Chagrian
Gregar Typho 185 NA
Cordé 157 NA
Cliegg Lars 183 Human
Poggle the Lesser 183 Geonosian
Luminara Unduli 170 Mirialan
Barriss Offee 166 Mirialan
Dormé 165 Human
Dooku 193 Human
Bail Prestor Organa 191 Human
Jango Fett 183 Human
Zam Wesell 168 Clawdite
Dexter Jettster 198 Besalisk
Lama Su 229 Kaminoan
Taun We 213 Kaminoan
Jocasta Nu 167 Human
R4-P17 96 Droid
Wat Tambor 193 Skakoan
San Hill 191 Muun
Shaak Ti 178 Togruta
Grievous 216 Kaleesh
Tarfful 234 Wookiee
Raymus Antilles 188 Human
Sly Moore 178 NA
Tion Medon 206 Pau’an
Finn NA Human
Rey NA Human
Poe Dameron NA Human
BB8 NA Droid
Captain Phasma NA Human

We can also deselect (multiple) columns:

df.starwars %>% 
  select(-name, -(birth_year:vehicles))
height mass hair_color skin_color eye_color starships
172 77.0 blond fair blue X-wing , Imperial shuttle
167 75.0 NA gold yellow
96 32.0 NA white, blue red
202 136.0 none white yellow TIE Advanced x1
150 49.0 brown light brown
178 120.0 brown, grey light blue
165 75.0 brown light blue
97 32.0 NA white, red red
183 84.0 black light brown X-wing
182 77.0 auburn, white fair blue-gray Jedi starfighter , Trade Federation cruiser, Naboo star skiff , Jedi Interceptor , Belbullab-22 starfighter
188 84.0 blond fair blue Naboo fighter , Trade Federation cruiser, Jedi Interceptor
180 NA auburn, grey fair blue
228 112.0 brown unknown blue Millennium Falcon, Imperial shuttle
180 80.0 brown fair brown Millennium Falcon, Imperial shuttle
173 74.0 NA green black
175 1358.0 NA green-tan, brown orange
170 77.0 brown fair hazel X-wing
180 110.0 brown fair blue X-wing
66 17.0 white green brown
170 75.0 grey pale yellow
183 78.2 black fair brown Slave 1
200 140.0 none metal red
190 113.0 none green red
177 79.0 black dark brown Millennium Falcon
175 79.0 none light blue
180 83.0 none brown mottle orange
150 NA auburn fair blue
NA NA brown fair brown A-wing
88 20.0 brown brown brown
160 68.0 none grey black Millennium Falcon
193 89.0 brown fair blue
191 90.0 none mottled green red
170 NA blond fair blue
185 45.0 brown light brown Naboo fighter , H-type Nubian yacht, Naboo star skiff
196 66.0 none orange orange
224 82.0 none grey orange
206 NA none green orange
183 NA brown fair blue Naboo Royal Starship
137 NA black blue, grey yellow
112 40.0 none grey, red orange
183 NA black dark brown
163 NA black fair brown
175 80.0 none red yellow Scimitar
180 NA none pale pink
178 55.0 none blue hazel
79 15.0 none grey, blue unknown
94 45.0 none blue, grey yellow
122 NA none white, blue black
163 65.0 none grey, green, yellow orange
188 84.0 none dark brown
198 82.0 white pale yellow
196 87.0 none green black
171 NA black brown brown
184 50.0 none dark blue
188 NA none pale orange
264 NA none white yellow
188 80.0 none orange black Jedi starfighter
196 NA none blue blue
185 85.0 black dark brown Naboo fighter
157 NA brown light brown
183 NA brown fair blue
183 80.0 none green yellow
170 56.2 black yellow blue
166 50.0 black yellow blue
165 NA brown light brown
193 80.0 white fair brown
191 NA black tan brown
183 79.0 black tan brown
168 55.0 blonde fair, green, yellow yellow
198 102.0 none brown yellow
229 88.0 none grey black
213 NA none grey black
167 NA white fair blue
96 NA none silver, red red, blue
193 48.0 none green, grey unknown
191 NA none grey gold
178 57.0 none red, blue, white black
216 159.0 none brown, white green, yellow Belbullab-22 starfighter
234 136.0 brown brown blue
188 79.0 brown light brown
178 48.0 none pale white
206 80.0 none grey black
NA NA black dark dark
NA NA brown light hazel
NA NA brown light brown X-wing
NA NA none none black
NA NA none none unknown

And select columns by partially matching the column name:

df.starwars %>% 
  select(contains("_")) # every column that contains the character "_"
hair_color skin_color eye_color birth_year
blond fair blue 19.0
NA gold yellow 112.0
NA white, blue red 33.0
none white yellow 41.9
brown light brown 19.0
brown, grey light blue 52.0
brown light blue 47.0
NA white, red red NA
black light brown 24.0
auburn, white fair blue-gray 57.0
blond fair blue 41.9
auburn, grey fair blue 64.0
brown unknown blue 200.0
brown fair brown 29.0
NA green black 44.0
NA green-tan, brown orange 600.0
brown fair hazel 21.0
brown fair blue NA
white green brown 896.0
grey pale yellow 82.0
black fair brown 31.5
none metal red 15.0
none green red 53.0
black dark brown 31.0
none light blue 37.0
none brown mottle orange 41.0
auburn fair blue 48.0
brown fair brown NA
brown brown brown 8.0
none grey black NA
brown fair blue 92.0
none mottled green red NA
blond fair blue 91.0
brown light brown 46.0
none orange orange 52.0
none grey orange NA
none green orange NA
brown fair blue NA
black blue, grey yellow NA
none grey, red orange NA
black dark brown 62.0
black fair brown 72.0
none red yellow 54.0
none pale pink NA
none blue hazel 48.0
none grey, blue unknown NA
none blue, grey yellow NA
none white, blue black NA
none grey, green, yellow orange NA
none dark brown 72.0
white pale yellow 92.0
none green black NA
black brown brown NA
none dark blue NA
none pale orange NA
none white yellow NA
none orange black 22.0
none blue blue NA
black dark brown NA
brown light brown NA
brown fair blue 82.0
none green yellow NA
black yellow blue 58.0
black yellow blue 40.0
brown light brown NA
white fair brown 102.0
black tan brown 67.0
black tan brown 66.0
blonde fair, green, yellow yellow NA
none brown yellow NA
none grey black NA
none grey black NA
white fair blue NA
none silver, red red, blue NA
none green, grey unknown NA
none grey gold NA
none red, blue, white black NA
none brown, white green, yellow NA
brown brown blue NA
brown light brown NA
none pale white NA
none grey black NA
black dark dark NA
brown light hazel NA
brown light brown NA
none none black NA
none none unknown NA
df.starwars %>% 
  select(starts_with("h")) # every column that starts with an "h"
height hair_color homeworld
172 blond Tatooine
167 NA Tatooine
96 NA Naboo
202 none Tatooine
150 brown Alderaan
178 brown, grey Tatooine
165 brown Tatooine
97 NA Tatooine
183 black Tatooine
182 auburn, white Stewjon
188 blond Tatooine
180 auburn, grey Eriadu
228 brown Kashyyyk
180 brown Corellia
173 NA Rodia
175 NA Nal Hutta
170 brown Corellia
180 brown Bestine IV
66 white NA
170 grey Naboo
183 black Kamino
200 none NA
190 none Trandosha
177 black Socorro
175 none Bespin
180 none Mon Cala
150 auburn Chandrila
NA brown NA
88 brown Endor
160 none Sullust
193 brown NA
191 none Cato Neimoidia
170 blond Coruscant
185 brown Naboo
196 none Naboo
224 none Naboo
206 none Naboo
183 brown Naboo
137 black Toydaria
112 none Malastare
183 black Naboo
163 black Tatooine
175 none Dathomir
180 none Ryloth
178 none Ryloth
79 none Aleen Minor
94 none Vulpter
122 none Troiken
163 none Tund
188 none Haruun Kal
198 white Cerea
196 none Glee Anselm
171 black Iridonia
184 none Coruscant
188 none Iktotch
264 none Quermia
188 none Dorin
196 none Champala
185 black Naboo
157 brown Naboo
183 brown Tatooine
183 none Geonosis
170 black Mirial
166 black Mirial
165 brown Naboo
193 white Serenno
191 black Alderaan
183 black Concord Dawn
168 blonde Zolan
198 none Ojom
229 none Kamino
213 none Kamino
167 white Coruscant
96 none NA
193 none Skako
191 none Muunilinst
178 none Shili
216 none Kalee
234 brown Kashyyyk
188 brown Alderaan
178 none Umbara
206 none Utapau
NA black NA
NA brown NA
NA brown NA
NA none NA
NA none NA

We can rename some of the columns using select() like so:

df.starwars %>% 
  select(person = name, height, mass_kg = mass)
person height mass_kg
Luke Skywalker 172 77.0
C-3PO 167 75.0
R2-D2 96 32.0
Darth Vader 202 136.0
Leia Organa 150 49.0
Owen Lars 178 120.0
Beru Whitesun Lars 165 75.0
R5-D4 97 32.0
Biggs Darklighter 183 84.0
Obi-Wan Kenobi 182 77.0
Anakin Skywalker 188 84.0
Wilhuff Tarkin 180 NA
Chewbacca 228 112.0
Han Solo 180 80.0
Greedo 173 74.0
Jabba Desilijic Tiure 175 1358.0
Wedge Antilles 170 77.0
Jek Tono Porkins 180 110.0
Yoda 66 17.0
Palpatine 170 75.0
Boba Fett 183 78.2
IG-88 200 140.0
Bossk 190 113.0
Lando Calrissian 177 79.0
Lobot 175 79.0
Ackbar 180 83.0
Mon Mothma 150 NA
Arvel Crynyd NA NA
Wicket Systri Warrick 88 20.0
Nien Nunb 160 68.0
Qui-Gon Jinn 193 89.0
Nute Gunray 191 90.0
Finis Valorum 170 NA
Padmé Amidala 185 45.0
Jar Jar Binks 196 66.0
Roos Tarpals 224 82.0
Rugor Nass 206 NA
Ric Olié 183 NA
Watto 137 NA
Sebulba 112 40.0
Quarsh Panaka 183 NA
Shmi Skywalker 163 NA
Darth Maul 175 80.0
Bib Fortuna 180 NA
Ayla Secura 178 55.0
Ratts Tyerel 79 15.0
Dud Bolt 94 45.0
Gasgano 122 NA
Ben Quadinaros 163 65.0
Mace Windu 188 84.0
Ki-Adi-Mundi 198 82.0
Kit Fisto 196 87.0
Eeth Koth 171 NA
Adi Gallia 184 50.0
Saesee Tiin 188 NA
Yarael Poof 264 NA
Plo Koon 188 80.0
Mas Amedda 196 NA
Gregar Typho 185 85.0
Cordé 157 NA
Cliegg Lars 183 NA
Poggle the Lesser 183 80.0
Luminara Unduli 170 56.2
Barriss Offee 166 50.0
Dormé 165 NA
Dooku 193 80.0
Bail Prestor Organa 191 NA
Jango Fett 183 79.0
Zam Wesell 168 55.0
Dexter Jettster 198 102.0
Lama Su 229 88.0
Taun We 213 NA
Jocasta Nu 167 NA
R4-P17 96 NA
Wat Tambor 193 48.0
San Hill 191 NA
Shaak Ti 178 57.0
Grievous 216 159.0
Tarfful 234 136.0
Raymus Antilles 188 79.0
Sly Moore 178 48.0
Tion Medon 206 80.0
Finn NA NA
Rey NA NA
Poe Dameron NA NA
BB8 NA NA
Captain Phasma NA NA

where()

where() is a useful helper function that comes in handy, for example, when we want to select columns based on their data type.

df.starwars %>% 
  select(where(fn = is.numeric)) # just select numeric columns
height mass birth_year
172 77.0 19.0
167 75.0 112.0
96 32.0 33.0
202 136.0 41.9
150 49.0 19.0
178 120.0 52.0
165 75.0 47.0
97 32.0 NA
183 84.0 24.0
182 77.0 57.0
188 84.0 41.9
180 NA 64.0
228 112.0 200.0
180 80.0 29.0
173 74.0 44.0
175 1358.0 600.0
170 77.0 21.0
180 110.0 NA
66 17.0 896.0
170 75.0 82.0
183 78.2 31.5
200 140.0 15.0
190 113.0 53.0
177 79.0 31.0
175 79.0 37.0
180 83.0 41.0
150 NA 48.0
NA NA NA
88 20.0 8.0
160 68.0 NA
193 89.0 92.0
191 90.0 NA
170 NA 91.0
185 45.0 46.0
196 66.0 52.0
224 82.0 NA
206 NA NA
183 NA NA
137 NA NA
112 40.0 NA
183 NA 62.0
163 NA 72.0
175 80.0 54.0
180 NA NA
178 55.0 48.0
79 15.0 NA
94 45.0 NA
122 NA NA
163 65.0 NA
188 84.0 72.0
198 82.0 92.0
196 87.0 NA
171 NA NA
184 50.0 NA
188 NA NA
264 NA NA
188 80.0 22.0
196 NA NA
185 85.0 NA
157 NA NA
183 NA 82.0
183 80.0 NA
170 56.2 58.0
166 50.0 40.0
165 NA NA
193 80.0 102.0
191 NA 67.0
183 79.0 66.0
168 55.0 NA
198 102.0 NA
229 88.0 NA
213 NA NA
167 NA NA
96 NA NA
193 48.0 NA
191 NA NA
178 57.0 NA
216 159.0 NA
234 136.0 NA
188 79.0 NA
178 48.0 NA
206 80.0 NA
NA NA NA
NA NA NA
NA NA NA
NA NA NA
NA NA NA

The following selects all columns that are not numeric:

df.starwars %>% 
  select(where(fn = ~ !is.numeric(.))) # selects all columns that are not numeric
name hair_color skin_color eye_color sex gender homeworld species films vehicles starships
Luke Skywalker blond fair blue male masculine Tatooine Human A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith , The Force Awakens Snowspeeder , Imperial Speeder Bike X-wing , Imperial shuttle
C-3PO NA gold yellow none masculine Tatooine Droid A New Hope , The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith
R2-D2 NA white, blue red none masculine Naboo Droid A New Hope , The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith , The Force Awakens
Darth Vader none white yellow male masculine Tatooine Human A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith TIE Advanced x1
Leia Organa brown light brown female feminine Alderaan Human A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith , The Force Awakens Imperial Speeder Bike
Owen Lars brown, grey light blue male masculine Tatooine Human A New Hope , Attack of the Clones, Revenge of the Sith
Beru Whitesun Lars brown light blue female feminine Tatooine Human A New Hope , Attack of the Clones, Revenge of the Sith
R5-D4 NA white, red red none masculine Tatooine Droid A New Hope
Biggs Darklighter black light brown male masculine Tatooine Human A New Hope X-wing
Obi-Wan Kenobi auburn, white fair blue-gray male masculine Stewjon Human A New Hope , The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith Tribubble bongo Jedi starfighter , Trade Federation cruiser, Naboo star skiff , Jedi Interceptor , Belbullab-22 starfighter
Anakin Skywalker blond fair blue male masculine Tatooine Human The Phantom Menace , Attack of the Clones, Revenge of the Sith Zephyr-G swoop bike, XJ-6 airspeeder Naboo fighter , Trade Federation cruiser, Jedi Interceptor
Wilhuff Tarkin auburn, grey fair blue male masculine Eriadu Human A New Hope , Revenge of the Sith
Chewbacca brown unknown blue male masculine Kashyyyk Wookiee A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith , The Force Awakens AT-ST Millennium Falcon, Imperial shuttle
Han Solo brown fair brown male masculine Corellia Human A New Hope , The Empire Strikes Back, Return of the Jedi , The Force Awakens Millennium Falcon, Imperial shuttle
Greedo NA green black male masculine Rodia Rodian A New Hope
Jabba Desilijic Tiure NA green-tan, brown orange hermaphroditic masculine Nal Hutta Hutt A New Hope , Return of the Jedi, The Phantom Menace
Wedge Antilles brown fair hazel male masculine Corellia Human A New Hope , The Empire Strikes Back, Return of the Jedi Snowspeeder X-wing
Jek Tono Porkins brown fair blue NA NA Bestine IV NA A New Hope X-wing
Yoda white green brown male masculine NA Yoda’s species The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith
Palpatine grey pale yellow male masculine Naboo Human The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith
Boba Fett black fair brown male masculine Kamino Human The Empire Strikes Back, Return of the Jedi , Attack of the Clones Slave 1
IG-88 none metal red none masculine NA Droid The Empire Strikes Back
Bossk none green red male masculine Trandosha Trandoshan The Empire Strikes Back
Lando Calrissian black dark brown male masculine Socorro Human The Empire Strikes Back, Return of the Jedi Millennium Falcon
Lobot none light blue male masculine Bespin Human The Empire Strikes Back
Ackbar none brown mottle orange male masculine Mon Cala Mon Calamari Return of the Jedi, The Force Awakens
Mon Mothma auburn fair blue female feminine Chandrila Human Return of the Jedi
Arvel Crynyd brown fair brown male masculine NA Human Return of the Jedi A-wing
Wicket Systri Warrick brown brown brown male masculine Endor Ewok Return of the Jedi
Nien Nunb none grey black male masculine Sullust Sullustan Return of the Jedi Millennium Falcon
Qui-Gon Jinn brown fair blue male masculine NA Human The Phantom Menace Tribubble bongo
Nute Gunray none mottled green red male masculine Cato Neimoidia Neimodian The Phantom Menace , Attack of the Clones, Revenge of the Sith
Finis Valorum blond fair blue male masculine Coruscant Human The Phantom Menace
Padmé Amidala brown light brown female feminine Naboo Human The Phantom Menace , Attack of the Clones, Revenge of the Sith Naboo fighter , H-type Nubian yacht, Naboo star skiff
Jar Jar Binks none orange orange male masculine Naboo Gungan The Phantom Menace , Attack of the Clones
Roos Tarpals none grey orange male masculine Naboo Gungan The Phantom Menace
Rugor Nass none green orange male masculine Naboo Gungan The Phantom Menace
Ric Olié brown fair blue male masculine Naboo Human The Phantom Menace Naboo Royal Starship
Watto black blue, grey yellow male masculine Toydaria Toydarian The Phantom Menace , Attack of the Clones
Sebulba none grey, red orange male masculine Malastare Dug The Phantom Menace
Quarsh Panaka black dark brown male masculine Naboo Human The Phantom Menace
Shmi Skywalker black fair brown female feminine Tatooine Human The Phantom Menace , Attack of the Clones
Darth Maul none red yellow male masculine Dathomir Zabrak The Phantom Menace Sith speeder Scimitar
Bib Fortuna none pale pink male masculine Ryloth Twi’lek Return of the Jedi
Ayla Secura none blue hazel female feminine Ryloth Twi’lek The Phantom Menace , Attack of the Clones, Revenge of the Sith
Ratts Tyerel none grey, blue unknown male masculine Aleen Minor Aleena The Phantom Menace
Dud Bolt none blue, grey yellow male masculine Vulpter Vulptereen The Phantom Menace
Gasgano none white, blue black male masculine Troiken Xexto The Phantom Menace
Ben Quadinaros none grey, green, yellow orange male masculine Tund Toong The Phantom Menace
Mace Windu none dark brown male masculine Haruun Kal Human The Phantom Menace , Attack of the Clones, Revenge of the Sith
Ki-Adi-Mundi white pale yellow male masculine Cerea Cerean The Phantom Menace , Attack of the Clones, Revenge of the Sith
Kit Fisto none green black male masculine Glee Anselm Nautolan The Phantom Menace , Attack of the Clones, Revenge of the Sith
Eeth Koth black brown brown male masculine Iridonia Zabrak The Phantom Menace , Revenge of the Sith
Adi Gallia none dark blue female feminine Coruscant Tholothian The Phantom Menace , Revenge of the Sith
Saesee Tiin none pale orange male masculine Iktotch Iktotchi The Phantom Menace , Revenge of the Sith
Yarael Poof none white yellow male masculine Quermia Quermian The Phantom Menace
Plo Koon none orange black male masculine Dorin Kel Dor The Phantom Menace , Attack of the Clones, Revenge of the Sith Jedi starfighter
Mas Amedda none blue blue male masculine Champala Chagrian The Phantom Menace , Attack of the Clones
Gregar Typho black dark brown NA NA Naboo NA Attack of the Clones Naboo fighter
Cordé brown light brown NA NA Naboo NA Attack of the Clones
Cliegg Lars brown fair blue male masculine Tatooine Human Attack of the Clones
Poggle the Lesser none green yellow male masculine Geonosis Geonosian Attack of the Clones, Revenge of the Sith
Luminara Unduli black yellow blue female feminine Mirial Mirialan Attack of the Clones, Revenge of the Sith
Barriss Offee black yellow blue female feminine Mirial Mirialan Attack of the Clones
Dormé brown light brown female feminine Naboo Human Attack of the Clones
Dooku white fair brown male masculine Serenno Human Attack of the Clones, Revenge of the Sith Flitknot speeder
Bail Prestor Organa black tan brown male masculine Alderaan Human Attack of the Clones, Revenge of the Sith
Jango Fett black tan brown male masculine Concord Dawn Human Attack of the Clones
Zam Wesell blonde fair, green, yellow yellow female feminine Zolan Clawdite Attack of the Clones Koro-2 Exodrive airspeeder
Dexter Jettster none brown yellow male masculine Ojom Besalisk Attack of the Clones
Lama Su none grey black male masculine Kamino Kaminoan Attack of the Clones
Taun We none grey black female feminine Kamino Kaminoan Attack of the Clones
Jocasta Nu white fair blue female feminine Coruscant Human Attack of the Clones
R4-P17 none silver, red red, blue none feminine NA Droid Attack of the Clones, Revenge of the Sith
Wat Tambor none green, grey unknown male masculine Skako Skakoan Attack of the Clones
San Hill none grey gold male masculine Muunilinst Muun Attack of the Clones
Shaak Ti none red, blue, white black female feminine Shili Togruta Attack of the Clones, Revenge of the Sith
Grievous none brown, white green, yellow male masculine Kalee Kaleesh Revenge of the Sith Tsmeu-6 personal wheel bike Belbullab-22 starfighter
Tarfful brown brown blue male masculine Kashyyyk Wookiee Revenge of the Sith
Raymus Antilles brown light brown male masculine Alderaan Human A New Hope , Revenge of the Sith
Sly Moore none pale white NA NA Umbara NA Attack of the Clones, Revenge of the Sith
Tion Medon none grey black male masculine Utapau Pau’an Revenge of the Sith
Finn black dark dark male masculine NA Human The Force Awakens
Rey brown light hazel female feminine NA Human The Force Awakens
Poe Dameron brown light brown male masculine NA Human The Force Awakens X-wing
BB8 none none black none masculine NA Droid The Force Awakens
Captain Phasma none none unknown female feminine NA Human The Force Awakens

Note that I used ~ here to indicate that I’m creating an anonymous function to check whether column type is numeric. A one-sided formula (expression beginning with ~) is interpreted as function(x), and wherever x would go in the function is represented by ..

So, I could write the same code like so:

df.starwars %>% 
  select(where(function(x) !is.numeric(x))) # selects all columns that are not numeric
name hair_color skin_color eye_color sex gender homeworld species films vehicles starships
Luke Skywalker blond fair blue male masculine Tatooine Human A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith , The Force Awakens Snowspeeder , Imperial Speeder Bike X-wing , Imperial shuttle
C-3PO NA gold yellow none masculine Tatooine Droid A New Hope , The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith
R2-D2 NA white, blue red none masculine Naboo Droid A New Hope , The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith , The Force Awakens
Darth Vader none white yellow male masculine Tatooine Human A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith TIE Advanced x1
Leia Organa brown light brown female feminine Alderaan Human A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith , The Force Awakens Imperial Speeder Bike
Owen Lars brown, grey light blue male masculine Tatooine Human A New Hope , Attack of the Clones, Revenge of the Sith
Beru Whitesun Lars brown light blue female feminine Tatooine Human A New Hope , Attack of the Clones, Revenge of the Sith
R5-D4 NA white, red red none masculine Tatooine Droid A New Hope
Biggs Darklighter black light brown male masculine Tatooine Human A New Hope X-wing
Obi-Wan Kenobi auburn, white fair blue-gray male masculine Stewjon Human A New Hope , The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith Tribubble bongo Jedi starfighter , Trade Federation cruiser, Naboo star skiff , Jedi Interceptor , Belbullab-22 starfighter
Anakin Skywalker blond fair blue male masculine Tatooine Human The Phantom Menace , Attack of the Clones, Revenge of the Sith Zephyr-G swoop bike, XJ-6 airspeeder Naboo fighter , Trade Federation cruiser, Jedi Interceptor
Wilhuff Tarkin auburn, grey fair blue male masculine Eriadu Human A New Hope , Revenge of the Sith
Chewbacca brown unknown blue male masculine Kashyyyk Wookiee A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith , The Force Awakens AT-ST Millennium Falcon, Imperial shuttle
Han Solo brown fair brown male masculine Corellia Human A New Hope , The Empire Strikes Back, Return of the Jedi , The Force Awakens Millennium Falcon, Imperial shuttle
Greedo NA green black male masculine Rodia Rodian A New Hope
Jabba Desilijic Tiure NA green-tan, brown orange hermaphroditic masculine Nal Hutta Hutt A New Hope , Return of the Jedi, The Phantom Menace
Wedge Antilles brown fair hazel male masculine Corellia Human A New Hope , The Empire Strikes Back, Return of the Jedi Snowspeeder X-wing
Jek Tono Porkins brown fair blue NA NA Bestine IV NA A New Hope X-wing
Yoda white green brown male masculine NA Yoda’s species The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith
Palpatine grey pale yellow male masculine Naboo Human The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith
Boba Fett black fair brown male masculine Kamino Human The Empire Strikes Back, Return of the Jedi , Attack of the Clones Slave 1
IG-88 none metal red none masculine NA Droid The Empire Strikes Back
Bossk none green red male masculine Trandosha Trandoshan The Empire Strikes Back
Lando Calrissian black dark brown male masculine Socorro Human The Empire Strikes Back, Return of the Jedi Millennium Falcon
Lobot none light blue male masculine Bespin Human The Empire Strikes Back
Ackbar none brown mottle orange male masculine Mon Cala Mon Calamari Return of the Jedi, The Force Awakens
Mon Mothma auburn fair blue female feminine Chandrila Human Return of the Jedi
Arvel Crynyd brown fair brown male masculine NA Human Return of the Jedi A-wing
Wicket Systri Warrick brown brown brown male masculine Endor Ewok Return of the Jedi
Nien Nunb none grey black male masculine Sullust Sullustan Return of the Jedi Millennium Falcon
Qui-Gon Jinn brown fair blue male masculine NA Human The Phantom Menace Tribubble bongo
Nute Gunray none mottled green red male masculine Cato Neimoidia Neimodian The Phantom Menace , Attack of the Clones, Revenge of the Sith
Finis Valorum blond fair blue male masculine Coruscant Human The Phantom Menace
Padmé Amidala brown light brown female feminine Naboo Human The Phantom Menace , Attack of the Clones, Revenge of the Sith Naboo fighter , H-type Nubian yacht, Naboo star skiff
Jar Jar Binks none orange orange male masculine Naboo Gungan The Phantom Menace , Attack of the Clones
Roos Tarpals none grey orange male masculine Naboo Gungan The Phantom Menace
Rugor Nass none green orange male masculine Naboo Gungan The Phantom Menace
Ric Olié brown fair blue male masculine Naboo Human The Phantom Menace Naboo Royal Starship
Watto black blue, grey yellow male masculine Toydaria Toydarian The Phantom Menace , Attack of the Clones
Sebulba none grey, red orange male masculine Malastare Dug The Phantom Menace
Quarsh Panaka black dark brown male masculine Naboo Human The Phantom Menace
Shmi Skywalker black fair brown female feminine Tatooine Human The Phantom Menace , Attack of the Clones
Darth Maul none red yellow male masculine Dathomir Zabrak The Phantom Menace Sith speeder Scimitar
Bib Fortuna none pale pink male masculine Ryloth Twi’lek Return of the Jedi
Ayla Secura none blue hazel female feminine Ryloth Twi’lek The Phantom Menace , Attack of the Clones, Revenge of the Sith
Ratts Tyerel none grey, blue unknown male masculine Aleen Minor Aleena The Phantom Menace
Dud Bolt none blue, grey yellow male masculine Vulpter Vulptereen The Phantom Menace
Gasgano none white, blue black male masculine Troiken Xexto The Phantom Menace
Ben Quadinaros none grey, green, yellow orange male masculine Tund Toong The Phantom Menace
Mace Windu none dark brown male masculine Haruun Kal Human The Phantom Menace , Attack of the Clones, Revenge of the Sith
Ki-Adi-Mundi white pale yellow male masculine Cerea Cerean The Phantom Menace , Attack of the Clones, Revenge of the Sith
Kit Fisto none green black male masculine Glee Anselm Nautolan The Phantom Menace , Attack of the Clones, Revenge of the Sith
Eeth Koth black brown brown male masculine Iridonia Zabrak The Phantom Menace , Revenge of the Sith
Adi Gallia none dark blue female feminine Coruscant Tholothian The Phantom Menace , Revenge of the Sith
Saesee Tiin none pale orange male masculine Iktotch Iktotchi The Phantom Menace , Revenge of the Sith
Yarael Poof none white yellow male masculine Quermia Quermian The Phantom Menace
Plo Koon none orange black male masculine Dorin Kel Dor The Phantom Menace , Attack of the Clones, Revenge of the Sith Jedi starfighter
Mas Amedda none blue blue male masculine Champala Chagrian The Phantom Menace , Attack of the Clones
Gregar Typho black dark brown NA NA Naboo NA Attack of the Clones Naboo fighter
Cordé brown light brown NA NA Naboo NA Attack of the Clones
Cliegg Lars brown fair blue male masculine Tatooine Human Attack of the Clones
Poggle the Lesser none green yellow male masculine Geonosis Geonosian Attack of the Clones, Revenge of the Sith
Luminara Unduli black yellow blue female feminine Mirial Mirialan Attack of the Clones, Revenge of the Sith
Barriss Offee black yellow blue female feminine Mirial Mirialan Attack of the Clones
Dormé brown light brown female feminine Naboo Human Attack of the Clones
Dooku white fair brown male masculine Serenno Human Attack of the Clones, Revenge of the Sith Flitknot speeder
Bail Prestor Organa black tan brown male masculine Alderaan Human Attack of the Clones, Revenge of the Sith
Jango Fett black tan brown male masculine Concord Dawn Human Attack of the Clones
Zam Wesell blonde fair, green, yellow yellow female feminine Zolan Clawdite Attack of the Clones Koro-2 Exodrive airspeeder
Dexter Jettster none brown yellow male masculine Ojom Besalisk Attack of the Clones
Lama Su none grey black male masculine Kamino Kaminoan Attack of the Clones
Taun We none grey black female feminine Kamino Kaminoan Attack of the Clones
Jocasta Nu white fair blue female feminine Coruscant Human Attack of the Clones
R4-P17 none silver, red red, blue none feminine NA Droid Attack of the Clones, Revenge of the Sith
Wat Tambor none green, grey unknown male masculine Skako Skakoan Attack of the Clones
San Hill none grey gold male masculine Muunilinst Muun Attack of the Clones
Shaak Ti none red, blue, white black female feminine Shili Togruta Attack of the Clones, Revenge of the Sith
Grievous none brown, white green, yellow male masculine Kalee Kaleesh Revenge of the Sith Tsmeu-6 personal wheel bike Belbullab-22 starfighter
Tarfful brown brown blue male masculine Kashyyyk Wookiee Revenge of the Sith
Raymus Antilles brown light brown male masculine Alderaan Human A New Hope , Revenge of the Sith
Sly Moore none pale white NA NA Umbara NA Attack of the Clones, Revenge of the Sith
Tion Medon none grey black male masculine Utapau Pau’an Revenge of the Sith
Finn black dark dark male masculine NA Human The Force Awakens
Rey brown light hazel female feminine NA Human The Force Awakens
Poe Dameron brown light brown male masculine NA Human The Force Awakens X-wing
BB8 none none black none masculine NA Droid The Force Awakens
Captain Phasma none none unknown female feminine NA Human The Force Awakens

For more details, take a look at the help file for select(), and this this great tutorial in which I learned about some of the more advanced ways of using select().

Practice 2

Create a data frame that: - only has the species Human and Droid - with the following data columns (in this order): name, species, birth_year, homeworld - is arranged according to birth year (with the lowest entry at the top of the data frame) - and has the name column renamed to person

# write your code here 

mutate()

mutate() is used to change existing columns or make new ones.

df.starwars %>% 
  mutate(height = height / 100, # to get height in meters
         bmi = mass / (height^2)) %>% # bmi = kg / (m^2)
  select(name, height, mass, bmi)
name height mass bmi
Luke Skywalker 1.72 77.0 26.02758
C-3PO 1.67 75.0 26.89232
R2-D2 0.96 32.0 34.72222
Darth Vader 2.02 136.0 33.33007
Leia Organa 1.50 49.0 21.77778
Owen Lars 1.78 120.0 37.87401
Beru Whitesun Lars 1.65 75.0 27.54821
R5-D4 0.97 32.0 34.00999
Biggs Darklighter 1.83 84.0 25.08286
Obi-Wan Kenobi 1.82 77.0 23.24598
Anakin Skywalker 1.88 84.0 23.76641
Wilhuff Tarkin 1.80 NA NA
Chewbacca 2.28 112.0 21.54509
Han Solo 1.80 80.0 24.69136
Greedo 1.73 74.0 24.72518
Jabba Desilijic Tiure 1.75 1358.0 443.42857
Wedge Antilles 1.70 77.0 26.64360
Jek Tono Porkins 1.80 110.0 33.95062
Yoda 0.66 17.0 39.02663
Palpatine 1.70 75.0 25.95156
Boba Fett 1.83 78.2 23.35095
IG-88 2.00 140.0 35.00000
Bossk 1.90 113.0 31.30194
Lando Calrissian 1.77 79.0 25.21625
Lobot 1.75 79.0 25.79592
Ackbar 1.80 83.0 25.61728
Mon Mothma 1.50 NA NA
Arvel Crynyd NA NA NA
Wicket Systri Warrick 0.88 20.0 25.82645
Nien Nunb 1.60 68.0 26.56250
Qui-Gon Jinn 1.93 89.0 23.89326
Nute Gunray 1.91 90.0 24.67038
Finis Valorum 1.70 NA NA
Padmé Amidala 1.85 45.0 13.14828
Jar Jar Binks 1.96 66.0 17.18034
Roos Tarpals 2.24 82.0 16.34247
Rugor Nass 2.06 NA NA
Ric Olié 1.83 NA NA
Watto 1.37 NA NA
Sebulba 1.12 40.0 31.88776
Quarsh Panaka 1.83 NA NA
Shmi Skywalker 1.63 NA NA
Darth Maul 1.75 80.0 26.12245
Bib Fortuna 1.80 NA NA
Ayla Secura 1.78 55.0 17.35892
Ratts Tyerel 0.79 15.0 24.03461
Dud Bolt 0.94 45.0 50.92802
Gasgano 1.22 NA NA
Ben Quadinaros 1.63 65.0 24.46460
Mace Windu 1.88 84.0 23.76641
Ki-Adi-Mundi 1.98 82.0 20.91623
Kit Fisto 1.96 87.0 22.64681
Eeth Koth 1.71 NA NA
Adi Gallia 1.84 50.0 14.76843
Saesee Tiin 1.88 NA NA
Yarael Poof 2.64 NA NA
Plo Koon 1.88 80.0 22.63468
Mas Amedda 1.96 NA NA
Gregar Typho 1.85 85.0 24.83565
Cordé 1.57 NA NA
Cliegg Lars 1.83 NA NA
Poggle the Lesser 1.83 80.0 23.88844
Luminara Unduli 1.70 56.2 19.44637
Barriss Offee 1.66 50.0 18.14487
Dormé 1.65 NA NA
Dooku 1.93 80.0 21.47709
Bail Prestor Organa 1.91 NA NA
Jango Fett 1.83 79.0 23.58984
Zam Wesell 1.68 55.0 19.48696
Dexter Jettster 1.98 102.0 26.01775
Lama Su 2.29 88.0 16.78076
Taun We 2.13 NA NA
Jocasta Nu 1.67 NA NA
R4-P17 0.96 NA NA
Wat Tambor 1.93 48.0 12.88625
San Hill 1.91 NA NA
Shaak Ti 1.78 57.0 17.99015
Grievous 2.16 159.0 34.07922
Tarfful 2.34 136.0 24.83746
Raymus Antilles 1.88 79.0 22.35174
Sly Moore 1.78 48.0 15.14960
Tion Medon 2.06 80.0 18.85192
Finn NA NA NA
Rey NA NA NA
Poe Dameron NA NA NA
BB8 NA NA NA
Captain Phasma NA NA NA

Here, I’ve calculated the bmi for the different starwars characters. I first mutated the height variable by going from cm to m, and then created the new column “bmi”.

A useful helper function for mutate() is ifelse() which is a shorthand for the if-else control flow (Section @ref(if-else)). Here is an example:

df.starwars %>% 
  mutate(height_categorical = ifelse(height > median(height, na.rm = T),
                                     "tall",
                                     "short")) %>% 
  select(name, contains("height"))
name height height_categorical
Luke Skywalker 172 short
C-3PO 167 short
R2-D2 96 short
Darth Vader 202 tall
Leia Organa 150 short
Owen Lars 178 short
Beru Whitesun Lars 165 short
R5-D4 97 short
Biggs Darklighter 183 tall
Obi-Wan Kenobi 182 tall
Anakin Skywalker 188 tall
Wilhuff Tarkin 180 short
Chewbacca 228 tall
Han Solo 180 short
Greedo 173 short
Jabba Desilijic Tiure 175 short
Wedge Antilles 170 short
Jek Tono Porkins 180 short
Yoda 66 short
Palpatine 170 short
Boba Fett 183 tall
IG-88 200 tall
Bossk 190 tall
Lando Calrissian 177 short
Lobot 175 short
Ackbar 180 short
Mon Mothma 150 short
Arvel Crynyd NA NA
Wicket Systri Warrick 88 short
Nien Nunb 160 short
Qui-Gon Jinn 193 tall
Nute Gunray 191 tall
Finis Valorum 170 short
Padmé Amidala 185 tall
Jar Jar Binks 196 tall
Roos Tarpals 224 tall
Rugor Nass 206 tall
Ric Olié 183 tall
Watto 137 short
Sebulba 112 short
Quarsh Panaka 183 tall
Shmi Skywalker 163 short
Darth Maul 175 short
Bib Fortuna 180 short
Ayla Secura 178 short
Ratts Tyerel 79 short
Dud Bolt 94 short
Gasgano 122 short
Ben Quadinaros 163 short
Mace Windu 188 tall
Ki-Adi-Mundi 198 tall
Kit Fisto 196 tall
Eeth Koth 171 short
Adi Gallia 184 tall
Saesee Tiin 188 tall
Yarael Poof 264 tall
Plo Koon 188 tall
Mas Amedda 196 tall
Gregar Typho 185 tall
Cordé 157 short
Cliegg Lars 183 tall
Poggle the Lesser 183 tall
Luminara Unduli 170 short
Barriss Offee 166 short
Dormé 165 short
Dooku 193 tall
Bail Prestor Organa 191 tall
Jango Fett 183 tall
Zam Wesell 168 short
Dexter Jettster 198 tall
Lama Su 229 tall
Taun We 213 tall
Jocasta Nu 167 short
R4-P17 96 short
Wat Tambor 193 tall
San Hill 191 tall
Shaak Ti 178 short
Grievous 216 tall
Tarfful 234 tall
Raymus Antilles 188 tall
Sly Moore 178 short
Tion Medon 206 tall
Finn NA NA
Rey NA NA
Poe Dameron NA NA
BB8 NA NA
Captain Phasma NA NA

ifelse() works in the following way: we first specify the condition, then what should be returned if the condition is true, and finally what should be returned otherwise. The more verbose version of the statement above would be: ifelse(test = height > median(height, na.rm = T), yes = "tall", no = "short")

In previous versions of dplyr (the package we use for data wrangling), there were a variety of additional mutate functions such as mutate_at(), mutate_if(), and mutate_all(). In the most recent version of dplyr, these additional functions have been deprecated, and replaced with the flexible across() helper function.

across()

across() allows us to use the syntax that we’ve learned for select() to select particular variables and apply a function to each of the selected variables.

For example, let’s imagine that we want to z-score a number of variables in our data frame. We can do this like so:

df.starwars %>%  
  mutate(across(.cols = c(height, mass, birth_year),
                .fns = scale))
name height mass hair_color skin_color eye_color birth_year sex gender homeworld species films vehicles starships
Luke Skywalker -0.07491018 -0.11986430 blond fair blue -0.44323795 male masculine Tatooine Human A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith , The Force Awakens Snowspeeder , Imperial Speeder Bike X-wing , Imperial shuttle
C-3PO -0.21869512 -0.13166669 NA gold yellow 0.15795886 none masculine Tatooine Droid A New Hope , The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith
R2-D2 -2.26044125 -0.38541814 NA white, blue red -0.35273520 none masculine Naboo Droid A New Hope , The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith , The Force Awakens
Darth Vader 0.78779946 0.22830629 none white yellow -0.29520131 male masculine Tatooine Human A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith TIE Advanced x1
Leia Organa -0.70756391 -0.28509780 brown light brown -0.44323795 female feminine Alderaan Human A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith , The Force Awakens Imperial Speeder Bike
Owen Lars 0.09763175 0.13388714 brown, grey light blue -0.22991005 male masculine Tatooine Human A New Hope , Attack of the Clones, Revenge of the Sith
Beru Whitesun Lars -0.27620909 -0.13166669 brown light blue -0.26223246 female feminine Tatooine Human A New Hope , Attack of the Clones, Revenge of the Sith
R5-D4 -2.23168426 -0.38541814 NA white, red red NA none masculine Tatooine Droid A New Hope
Biggs Darklighter 0.24141669 -0.07855593 black light brown -0.41091554 male masculine Tatooine Human A New Hope X-wing
Obi-Wan Kenobi 0.21265970 -0.11986430 auburn, white fair blue-gray -0.19758764 male masculine Stewjon Human A New Hope , The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith Tribubble bongo Jedi starfighter , Trade Federation cruiser, Naboo star skiff , Jedi Interceptor , Belbullab-22 starfighter
Anakin Skywalker 0.38520163 -0.07855593 blond fair blue -0.29520131 male masculine Tatooine Human The Phantom Menace , Attack of the Clones, Revenge of the Sith Zephyr-G swoop bike, XJ-6 airspeeder Naboo fighter , Trade Federation cruiser, Jedi Interceptor
Wilhuff Tarkin 0.15514572 NA auburn, grey fair blue -0.15233627 male masculine Eriadu Human A New Hope , Revenge of the Sith
Chewbacca 1.53548114 0.08667757 brown unknown blue 0.72683326 male masculine Kashyyyk Wookiee A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith , The Force Awakens AT-ST Millennium Falcon, Imperial shuttle
Han Solo 0.15514572 -0.10216071 brown fair brown -0.37859313 male masculine Corellia Human A New Hope , The Empire Strikes Back, Return of the Jedi , The Force Awakens Millennium Falcon, Imperial shuttle
Greedo -0.04615319 -0.13756789 NA green black -0.28162590 male masculine Rodia Rodian A New Hope
Jabba Desilijic Tiure 0.01136079 7.43956829 NA green-tan, brown orange 3.31262600 hermaphroditic masculine Nal Hutta Hutt A New Hope , Return of the Jedi, The Phantom Menace
Wedge Antilles -0.13242415 -0.11986430 brown fair hazel -0.43030899 male masculine Corellia Human A New Hope , The Empire Strikes Back, Return of the Jedi Snowspeeder X-wing
Jek Tono Porkins 0.15514572 0.07487518 brown fair blue NA NA NA Bestine IV NA A New Hope X-wing
Yoda -3.12315088 -0.47393609 white green brown 5.22611263 male masculine NA Yoda’s species The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith
Palpatine -0.13242415 -0.13166669 grey pale yellow -0.03597559 male masculine Naboo Human The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith
Boba Fett 0.24141669 -0.11278287 black fair brown -0.36243193 male masculine Kamino Human The Empire Strikes Back, Return of the Jedi , Attack of the Clones Slave 1
IG-88 0.73028548 0.25191107 none metal red -0.46909588 none masculine NA Droid The Empire Strikes Back
Bossk 0.44271560 0.09257877 none green red -0.22344557 male masculine Trandosha Trandoshan The Empire Strikes Back
Lando Calrissian 0.06887476 -0.10806191 black dark brown -0.36566417 male masculine Socorro Human The Empire Strikes Back, Return of the Jedi Millennium Falcon
Lobot 0.01136079 -0.10806191 none light blue -0.32687728 male masculine Bespin Human The Empire Strikes Back
Ackbar 0.15514572 -0.08445712 none brown mottle orange -0.30101935 male masculine Mon Cala Mon Calamari Return of the Jedi, The Force Awakens
Mon Mothma -0.70756391 NA auburn fair blue -0.25576798 female feminine Chandrila Human Return of the Jedi
Arvel Crynyd NA NA brown fair brown NA male masculine NA Human Return of the Jedi A-wing
Wicket Systri Warrick -2.49049715 -0.45623250 brown brown brown -0.51434725 male masculine Endor Ewok Return of the Jedi
Nien Nunb -0.41999403 -0.17297507 none grey black NA male masculine Sullust Sullustan Return of the Jedi Millennium Falcon
Qui-Gon Jinn 0.52898657 -0.04904994 brown fair blue 0.02866923 male masculine NA Human The Phantom Menace Tribubble bongo
Nute Gunray 0.47147259 -0.04314875 none mottled green red NA male masculine Cato Neimoidia Neimodian The Phantom Menace , Attack of the Clones, Revenge of the Sith
Finis Valorum -0.13242415 NA blond fair blue 0.02220474 male masculine Coruscant Human The Phantom Menace
Padmé Amidala 0.29893066 -0.30870259 brown light brown -0.26869694 female feminine Naboo Human The Phantom Menace , Attack of the Clones, Revenge of the Sith Naboo fighter , H-type Nubian yacht, Naboo star skiff
Jar Jar Binks 0.61525753 -0.18477746 none orange orange -0.22991005 male masculine Naboo Gungan The Phantom Menace , Attack of the Clones
Roos Tarpals 1.42045319 -0.09035832 none grey orange NA male masculine Naboo Gungan The Phantom Menace
Rugor Nass 0.90282741 NA none green orange NA male masculine Naboo Gungan The Phantom Menace
Ric Olié 0.24141669 NA brown fair blue NA male masculine Naboo Human The Phantom Menace Naboo Royal Starship
Watto -1.08140475 NA black blue, grey yellow NA male masculine Toydaria Toydarian The Phantom Menace , Attack of the Clones
Sebulba -1.80032945 -0.33820857 none grey, red orange NA male masculine Malastare Dug The Phantom Menace
Quarsh Panaka 0.24141669 NA black dark brown -0.16526523 male masculine Naboo Human The Phantom Menace
Shmi Skywalker -0.33372307 NA black fair brown -0.10062041 female feminine Tatooine Human The Phantom Menace , Attack of the Clones
Darth Maul 0.01136079 -0.10216071 none red yellow -0.21698108 male masculine Dathomir Zabrak The Phantom Menace Sith speeder Scimitar
Bib Fortuna 0.15514572 NA none pale pink NA male masculine Ryloth Twi’lek Return of the Jedi
Ayla Secura 0.09763175 -0.24969062 none blue hazel -0.25576798 female feminine Ryloth Twi’lek The Phantom Menace , Attack of the Clones, Revenge of the Sith
Ratts Tyerel -2.74931004 -0.48573848 none grey, blue unknown NA male masculine Aleen Minor Aleena The Phantom Menace
Dud Bolt -2.31795523 -0.30870259 none blue, grey yellow NA male masculine Vulpter Vulptereen The Phantom Menace
Gasgano -1.51275957 NA none white, blue black NA male masculine Troiken Xexto The Phantom Menace
Ben Quadinaros -0.33372307 -0.19067866 none grey, green, yellow orange NA male masculine Tund Toong The Phantom Menace
Mace Windu 0.38520163 -0.07855593 none dark brown -0.10062041 male masculine Haruun Kal Human The Phantom Menace , Attack of the Clones, Revenge of the Sith
Ki-Adi-Mundi 0.67277150 -0.09035832 white pale yellow 0.02866923 male masculine Cerea Cerean The Phantom Menace , Attack of the Clones, Revenge of the Sith
Kit Fisto 0.61525753 -0.06085234 none green black NA male masculine Glee Anselm Nautolan The Phantom Menace , Attack of the Clones, Revenge of the Sith
Eeth Koth -0.10366717 NA black brown brown NA male masculine Iridonia Zabrak The Phantom Menace , Revenge of the Sith
Adi Gallia 0.27017368 -0.27919660 none dark blue NA female feminine Coruscant Tholothian The Phantom Menace , Revenge of the Sith
Saesee Tiin 0.38520163 NA none pale orange NA male masculine Iktotch Iktotchi The Phantom Menace , Revenge of the Sith
Yarael Poof 2.57073270 NA none white yellow NA male masculine Quermia Quermian The Phantom Menace
Plo Koon 0.38520163 -0.10216071 none orange black -0.42384450 male masculine Dorin Kel Dor The Phantom Menace , Attack of the Clones, Revenge of the Sith Jedi starfighter
Mas Amedda 0.61525753 NA none blue blue NA male masculine Champala Chagrian The Phantom Menace , Attack of the Clones
Gregar Typho 0.29893066 -0.07265473 black dark brown NA NA NA Naboo NA Attack of the Clones Naboo fighter
Cordé -0.50626499 NA brown light brown NA NA NA Naboo NA Attack of the Clones
Cliegg Lars 0.24141669 NA brown fair blue -0.03597559 male masculine Tatooine Human Attack of the Clones
Poggle the Lesser 0.24141669 -0.10216071 none green yellow NA male masculine Geonosis Geonosian Attack of the Clones, Revenge of the Sith
Luminara Unduli -0.13242415 -0.24260919 black yellow blue -0.19112316 female feminine Mirial Mirialan Attack of the Clones, Revenge of the Sith
Barriss Offee -0.24745210 -0.27919660 black yellow blue -0.30748383 female feminine Mirial Mirialan Attack of the Clones
Dormé -0.27620909 NA brown light brown NA female feminine Naboo Human Attack of the Clones
Dooku 0.52898657 -0.10216071 white fair brown 0.09331404 male masculine Serenno Human Attack of the Clones, Revenge of the Sith Flitknot speeder
Bail Prestor Organa 0.47147259 NA black tan brown -0.13294282 male masculine Alderaan Human Attack of the Clones, Revenge of the Sith
Jango Fett 0.24141669 -0.10806191 black tan brown -0.13940730 male masculine Concord Dawn Human Attack of the Clones
Zam Wesell -0.18993813 -0.24969062 blonde fair, green, yellow yellow NA female feminine Zolan Clawdite Attack of the Clones Koro-2 Exodrive airspeeder
Dexter Jettster 0.67277150 0.02766561 none brown yellow NA male masculine Ojom Besalisk Attack of the Clones
Lama Su 1.56423813 -0.05495114 none grey black NA male masculine Kamino Kaminoan Attack of the Clones
Taun We 1.10412632 NA none grey black NA female feminine Kamino Kaminoan Attack of the Clones
Jocasta Nu -0.21869512 NA white fair blue NA female feminine Coruscant Human Attack of the Clones
R4-P17 -2.26044125 NA none silver, red red, blue NA none feminine NA Droid Attack of the Clones, Revenge of the Sith
Wat Tambor 0.52898657 -0.29099900 none green, grey unknown NA male masculine Skako Skakoan Attack of the Clones
San Hill 0.47147259 NA none grey gold NA male masculine Muunilinst Muun Attack of the Clones
Shaak Ti 0.09763175 -0.23788823 none red, blue, white black NA female feminine Shili Togruta Attack of the Clones, Revenge of the Sith
Grievous 1.19039728 0.36403380 none brown, white green, yellow NA male masculine Kalee Kaleesh Revenge of the Sith Tsmeu-6 personal wheel bike Belbullab-22 starfighter
Tarfful 1.70802306 0.22830629 brown brown blue NA male masculine Kashyyyk Wookiee Revenge of the Sith
Raymus Antilles 0.38520163 -0.10806191 brown light brown NA male masculine Alderaan Human A New Hope , Revenge of the Sith
Sly Moore 0.09763175 -0.29099900 none pale white NA NA NA Umbara NA Attack of the Clones, Revenge of the Sith
Tion Medon 0.90282741 -0.10216071 none grey black NA male masculine Utapau Pau’an Revenge of the Sith
Finn NA NA black dark dark NA male masculine NA Human The Force Awakens
Rey NA NA brown light hazel NA female feminine NA Human The Force Awakens
Poe Dameron NA NA brown light brown NA male masculine NA Human The Force Awakens X-wing
BB8 NA NA none none black NA none masculine NA Droid The Force Awakens
Captain Phasma NA NA none none unknown NA female feminine NA Human The Force Awakens

In the .cols = argument of across(), I’ve specified what variables to mutate. In the .fns = argument, I’ve specified that I want to use the function scale. Note that I wrote the function without (). The .fns argument expects allows these possible values:

  • the function itself, e.g. mean
  • a call to the function with . as a dummy argument, ~ mean(.) (note the ~ before the function call)
  • a list of functions list(mean = mean, median = ~ median(.)) (where I’ve mixed both of the other ways)

We can also use names to create new columns:

df.starwars %>%  
  mutate(across(.cols = c(height, mass, birth_year),
                .fns = scale,
                .names = "{.col}_z")) %>% 
  select(name, contains("height"), contains("mass"), contains("birth_year"))
name height height_z mass mass_z birth_year birth_year_z
Luke Skywalker 172 -0.07491018 77.0 -0.11986430 19.0 -0.44323795
C-3PO 167 -0.21869512 75.0 -0.13166669 112.0 0.15795886
R2-D2 96 -2.26044125 32.0 -0.38541814 33.0 -0.35273520
Darth Vader 202 0.78779946 136.0 0.22830629 41.9 -0.29520131
Leia Organa 150 -0.70756391 49.0 -0.28509780 19.0 -0.44323795
Owen Lars 178 0.09763175 120.0 0.13388714 52.0 -0.22991005
Beru Whitesun Lars 165 -0.27620909 75.0 -0.13166669 47.0 -0.26223246
R5-D4 97 -2.23168426 32.0 -0.38541814 NA NA
Biggs Darklighter 183 0.24141669 84.0 -0.07855593 24.0 -0.41091554
Obi-Wan Kenobi 182 0.21265970 77.0 -0.11986430 57.0 -0.19758764
Anakin Skywalker 188 0.38520163 84.0 -0.07855593 41.9 -0.29520131
Wilhuff Tarkin 180 0.15514572 NA NA 64.0 -0.15233627
Chewbacca 228 1.53548114 112.0 0.08667757 200.0 0.72683326
Han Solo 180 0.15514572 80.0 -0.10216071 29.0 -0.37859313
Greedo 173 -0.04615319 74.0 -0.13756789 44.0 -0.28162590
Jabba Desilijic Tiure 175 0.01136079 1358.0 7.43956829 600.0 3.31262600
Wedge Antilles 170 -0.13242415 77.0 -0.11986430 21.0 -0.43030899
Jek Tono Porkins 180 0.15514572 110.0 0.07487518 NA NA
Yoda 66 -3.12315088 17.0 -0.47393609 896.0 5.22611263
Palpatine 170 -0.13242415 75.0 -0.13166669 82.0 -0.03597559
Boba Fett 183 0.24141669 78.2 -0.11278287 31.5 -0.36243193
IG-88 200 0.73028548 140.0 0.25191107 15.0 -0.46909588
Bossk 190 0.44271560 113.0 0.09257877 53.0 -0.22344557
Lando Calrissian 177 0.06887476 79.0 -0.10806191 31.0 -0.36566417
Lobot 175 0.01136079 79.0 -0.10806191 37.0 -0.32687728
Ackbar 180 0.15514572 83.0 -0.08445712 41.0 -0.30101935
Mon Mothma 150 -0.70756391 NA NA 48.0 -0.25576798
Arvel Crynyd NA NA NA NA NA NA
Wicket Systri Warrick 88 -2.49049715 20.0 -0.45623250 8.0 -0.51434725
Nien Nunb 160 -0.41999403 68.0 -0.17297507 NA NA
Qui-Gon Jinn 193 0.52898657 89.0 -0.04904994 92.0 0.02866923
Nute Gunray 191 0.47147259 90.0 -0.04314875 NA NA
Finis Valorum 170 -0.13242415 NA NA 91.0 0.02220474
Padmé Amidala 185 0.29893066 45.0 -0.30870259 46.0 -0.26869694
Jar Jar Binks 196 0.61525753 66.0 -0.18477746 52.0 -0.22991005
Roos Tarpals 224 1.42045319 82.0 -0.09035832 NA NA
Rugor Nass 206 0.90282741 NA NA NA NA
Ric Olié 183 0.24141669 NA NA NA NA
Watto 137 -1.08140475 NA NA NA NA
Sebulba 112 -1.80032945 40.0 -0.33820857 NA NA
Quarsh Panaka 183 0.24141669 NA NA 62.0 -0.16526523
Shmi Skywalker 163 -0.33372307 NA NA 72.0 -0.10062041
Darth Maul 175 0.01136079 80.0 -0.10216071 54.0 -0.21698108
Bib Fortuna 180 0.15514572 NA NA NA NA
Ayla Secura 178 0.09763175 55.0 -0.24969062 48.0 -0.25576798
Ratts Tyerel 79 -2.74931004 15.0 -0.48573848 NA NA
Dud Bolt 94 -2.31795523 45.0 -0.30870259 NA NA
Gasgano 122 -1.51275957 NA NA NA NA
Ben Quadinaros 163 -0.33372307 65.0 -0.19067866 NA NA
Mace Windu 188 0.38520163 84.0 -0.07855593 72.0 -0.10062041
Ki-Adi-Mundi 198 0.67277150 82.0 -0.09035832 92.0 0.02866923
Kit Fisto 196 0.61525753 87.0 -0.06085234 NA NA
Eeth Koth 171 -0.10366717 NA NA NA NA
Adi Gallia 184 0.27017368 50.0 -0.27919660 NA NA
Saesee Tiin 188 0.38520163 NA NA NA NA
Yarael Poof 264 2.57073270 NA NA NA NA
Plo Koon 188 0.38520163 80.0 -0.10216071 22.0 -0.42384450
Mas Amedda 196 0.61525753 NA NA NA NA
Gregar Typho 185 0.29893066 85.0 -0.07265473 NA NA
Cordé 157 -0.50626499 NA NA NA NA
Cliegg Lars 183 0.24141669 NA NA 82.0 -0.03597559
Poggle the Lesser 183 0.24141669 80.0 -0.10216071 NA NA
Luminara Unduli 170 -0.13242415 56.2 -0.24260919 58.0 -0.19112316
Barriss Offee 166 -0.24745210 50.0 -0.27919660 40.0 -0.30748383
Dormé 165 -0.27620909 NA NA NA NA
Dooku 193 0.52898657 80.0 -0.10216071 102.0 0.09331404
Bail Prestor Organa 191 0.47147259 NA NA 67.0 -0.13294282
Jango Fett 183 0.24141669 79.0 -0.10806191 66.0 -0.13940730
Zam Wesell 168 -0.18993813 55.0 -0.24969062 NA NA
Dexter Jettster 198 0.67277150 102.0 0.02766561 NA NA
Lama Su 229 1.56423813 88.0 -0.05495114 NA NA
Taun We 213 1.10412632 NA NA NA NA
Jocasta Nu 167 -0.21869512 NA NA NA NA
R4-P17 96 -2.26044125 NA NA NA NA
Wat Tambor 193 0.52898657 48.0 -0.29099900 NA NA
San Hill 191 0.47147259 NA NA NA NA
Shaak Ti 178 0.09763175 57.0 -0.23788823 NA NA
Grievous 216 1.19039728 159.0 0.36403380 NA NA
Tarfful 234 1.70802306 136.0 0.22830629 NA NA
Raymus Antilles 188 0.38520163 79.0 -0.10806191 NA NA
Sly Moore 178 0.09763175 48.0 -0.29099900 NA NA
Tion Medon 206 0.90282741 80.0 -0.10216071 NA NA
Finn NA NA NA NA NA NA
Rey NA NA NA NA NA NA
Poe Dameron NA NA NA NA NA NA
BB8 NA NA NA NA NA NA
Captain Phasma NA NA NA NA NA NA

I’ve specified how I’d like the new variables to be called by using the .names = argument of across(). {.col} stands of the name of the original column, and here I’ve just added _z to each column name for the scaled columns.

We can also apply several functions at the same time.

df.starwars %>% 
  mutate(across(.cols = c(height, mass, birth_year),
                .fns = list(z = scale,
                            centered = ~ scale(., scale = FALSE)))) %>%
  select(name, contains("height"), contains("mass"), contains("birth_year"))
name height height_z height_centered mass mass_z mass_centered birth_year birth_year_z birth_year_centered
Luke Skywalker 172 -0.07491018 -2.6049383 77.0 -0.11986430 -20.311864 19.0 -0.44323795 -68.565116
C-3PO 167 -0.21869512 -7.6049383 75.0 -0.13166669 -22.311864 112.0 0.15795886 24.434884
R2-D2 96 -2.26044125 -78.6049383 32.0 -0.38541814 -65.311864 33.0 -0.35273520 -54.565116
Darth Vader 202 0.78779946 27.3950617 136.0 0.22830629 38.688136 41.9 -0.29520131 -45.665116
Leia Organa 150 -0.70756391 -24.6049383 49.0 -0.28509780 -48.311864 19.0 -0.44323795 -68.565116
Owen Lars 178 0.09763175 3.3950617 120.0 0.13388714 22.688136 52.0 -0.22991005 -35.565116
Beru Whitesun Lars 165 -0.27620909 -9.6049383 75.0 -0.13166669 -22.311864 47.0 -0.26223246 -40.565116
R5-D4 97 -2.23168426 -77.6049383 32.0 -0.38541814 -65.311864 NA NA NA
Biggs Darklighter 183 0.24141669 8.3950617 84.0 -0.07855593 -13.311864 24.0 -0.41091554 -63.565116
Obi-Wan Kenobi 182 0.21265970 7.3950617 77.0 -0.11986430 -20.311864 57.0 -0.19758764 -30.565116
Anakin Skywalker 188 0.38520163 13.3950617 84.0 -0.07855593 -13.311864 41.9 -0.29520131 -45.665116
Wilhuff Tarkin 180 0.15514572 5.3950617 NA NA NA 64.0 -0.15233627 -23.565116
Chewbacca 228 1.53548114 53.3950617 112.0 0.08667757 14.688136 200.0 0.72683326 112.434884
Han Solo 180 0.15514572 5.3950617 80.0 -0.10216071 -17.311864 29.0 -0.37859313 -58.565116
Greedo 173 -0.04615319 -1.6049383 74.0 -0.13756789 -23.311864 44.0 -0.28162590 -43.565116
Jabba Desilijic Tiure 175 0.01136079 0.3950617 1358.0 7.43956829 1260.688136 600.0 3.31262600 512.434884
Wedge Antilles 170 -0.13242415 -4.6049383 77.0 -0.11986430 -20.311864 21.0 -0.43030899 -66.565116
Jek Tono Porkins 180 0.15514572 5.3950617 110.0 0.07487518 12.688136 NA NA NA
Yoda 66 -3.12315088 -108.6049383 17.0 -0.47393609 -80.311864 896.0 5.22611263 808.434884
Palpatine 170 -0.13242415 -4.6049383 75.0 -0.13166669 -22.311864 82.0 -0.03597559 -5.565116
Boba Fett 183 0.24141669 8.3950617 78.2 -0.11278287 -19.111864 31.5 -0.36243193 -56.065116
IG-88 200 0.73028548 25.3950617 140.0 0.25191107 42.688136 15.0 -0.46909588 -72.565116
Bossk 190 0.44271560 15.3950617 113.0 0.09257877 15.688136 53.0 -0.22344557 -34.565116
Lando Calrissian 177 0.06887476 2.3950617 79.0 -0.10806191 -18.311864 31.0 -0.36566417 -56.565116
Lobot 175 0.01136079 0.3950617 79.0 -0.10806191 -18.311864 37.0 -0.32687728 -50.565116
Ackbar 180 0.15514572 5.3950617 83.0 -0.08445712 -14.311864 41.0 -0.30101935 -46.565116
Mon Mothma 150 -0.70756391 -24.6049383 NA NA NA 48.0 -0.25576798 -39.565116
Arvel Crynyd NA NA NA NA NA NA NA NA NA
Wicket Systri Warrick 88 -2.49049715 -86.6049383 20.0 -0.45623250 -77.311864 8.0 -0.51434725 -79.565116
Nien Nunb 160 -0.41999403 -14.6049383 68.0 -0.17297507 -29.311864 NA NA NA
Qui-Gon Jinn 193 0.52898657 18.3950617 89.0 -0.04904994 -8.311864 92.0 0.02866923 4.434884
Nute Gunray 191 0.47147259 16.3950617 90.0 -0.04314875 -7.311864 NA NA NA
Finis Valorum 170 -0.13242415 -4.6049383 NA NA NA 91.0 0.02220474 3.434884
Padmé Amidala 185 0.29893066 10.3950617 45.0 -0.30870259 -52.311864 46.0 -0.26869694 -41.565116
Jar Jar Binks 196 0.61525753 21.3950617 66.0 -0.18477746 -31.311864 52.0 -0.22991005 -35.565116
Roos Tarpals 224 1.42045319 49.3950617 82.0 -0.09035832 -15.311864 NA NA NA
Rugor Nass 206 0.90282741 31.3950617 NA NA NA NA NA NA
Ric Olié 183 0.24141669 8.3950617 NA NA NA NA NA NA
Watto 137 -1.08140475 -37.6049383 NA NA NA NA NA NA
Sebulba 112 -1.80032945 -62.6049383 40.0 -0.33820857 -57.311864 NA NA NA
Quarsh Panaka 183 0.24141669 8.3950617 NA NA NA 62.0 -0.16526523 -25.565116
Shmi Skywalker 163 -0.33372307 -11.6049383 NA NA NA 72.0 -0.10062041 -15.565116
Darth Maul 175 0.01136079 0.3950617 80.0 -0.10216071 -17.311864 54.0 -0.21698108 -33.565116
Bib Fortuna 180 0.15514572 5.3950617 NA NA NA NA NA NA
Ayla Secura 178 0.09763175 3.3950617 55.0 -0.24969062 -42.311864 48.0 -0.25576798 -39.565116
Ratts Tyerel 79 -2.74931004 -95.6049383 15.0 -0.48573848 -82.311864 NA NA NA
Dud Bolt 94 -2.31795523 -80.6049383 45.0 -0.30870259 -52.311864 NA NA NA
Gasgano 122 -1.51275957 -52.6049383 NA NA NA NA NA NA
Ben Quadinaros 163 -0.33372307 -11.6049383 65.0 -0.19067866 -32.311864 NA NA NA
Mace Windu 188 0.38520163 13.3950617 84.0 -0.07855593 -13.311864 72.0 -0.10062041 -15.565116
Ki-Adi-Mundi 198 0.67277150 23.3950617 82.0 -0.09035832 -15.311864 92.0 0.02866923 4.434884
Kit Fisto 196 0.61525753 21.3950617 87.0 -0.06085234 -10.311864 NA NA NA
Eeth Koth 171 -0.10366717 -3.6049383 NA NA NA NA NA NA
Adi Gallia 184 0.27017368 9.3950617 50.0 -0.27919660 -47.311864 NA NA NA
Saesee Tiin 188 0.38520163 13.3950617 NA NA NA NA NA NA
Yarael Poof 264 2.57073270 89.3950617 NA NA NA NA NA NA
Plo Koon 188 0.38520163 13.3950617 80.0 -0.10216071 -17.311864 22.0 -0.42384450 -65.565116
Mas Amedda 196 0.61525753 21.3950617 NA NA NA NA NA NA
Gregar Typho 185 0.29893066 10.3950617 85.0 -0.07265473 -12.311864 NA NA NA
Cordé 157 -0.50626499 -17.6049383 NA NA NA NA NA NA
Cliegg Lars 183 0.24141669 8.3950617 NA NA NA 82.0 -0.03597559 -5.565116
Poggle the Lesser 183 0.24141669 8.3950617 80.0 -0.10216071 -17.311864 NA NA NA
Luminara Unduli 170 -0.13242415 -4.6049383 56.2 -0.24260919 -41.111864 58.0 -0.19112316 -29.565116
Barriss Offee 166 -0.24745210 -8.6049383 50.0 -0.27919660 -47.311864 40.0 -0.30748383 -47.565116
Dormé 165 -0.27620909 -9.6049383 NA NA NA NA NA NA
Dooku 193 0.52898657 18.3950617 80.0 -0.10216071 -17.311864 102.0 0.09331404 14.434884
Bail Prestor Organa 191 0.47147259 16.3950617 NA NA NA 67.0 -0.13294282 -20.565116
Jango Fett 183 0.24141669 8.3950617 79.0 -0.10806191 -18.311864 66.0 -0.13940730 -21.565116
Zam Wesell 168 -0.18993813 -6.6049383 55.0 -0.24969062 -42.311864 NA NA NA
Dexter Jettster 198 0.67277150 23.3950617 102.0 0.02766561 4.688136 NA NA NA
Lama Su 229 1.56423813 54.3950617 88.0 -0.05495114 -9.311864 NA NA NA
Taun We 213 1.10412632 38.3950617 NA NA NA NA NA NA
Jocasta Nu 167 -0.21869512 -7.6049383 NA NA NA NA NA NA
R4-P17 96 -2.26044125 -78.6049383 NA NA NA NA NA NA
Wat Tambor 193 0.52898657 18.3950617 48.0 -0.29099900 -49.311864 NA NA NA
San Hill 191 0.47147259 16.3950617 NA NA NA NA NA NA
Shaak Ti 178 0.09763175 3.3950617 57.0 -0.23788823 -40.311864 NA NA NA
Grievous 216 1.19039728 41.3950617 159.0 0.36403380 61.688136 NA NA NA
Tarfful 234 1.70802306 59.3950617 136.0 0.22830629 38.688136 NA NA NA
Raymus Antilles 188 0.38520163 13.3950617 79.0 -0.10806191 -18.311864 NA NA NA
Sly Moore 178 0.09763175 3.3950617 48.0 -0.29099900 -49.311864 NA NA NA
Tion Medon 206 0.90282741 31.3950617 80.0 -0.10216071 -17.311864 NA NA NA
Finn NA NA NA NA NA NA NA NA NA
Rey NA NA NA NA NA NA NA NA NA
Poe Dameron NA NA NA NA NA NA NA NA NA
BB8 NA NA NA NA NA NA NA NA NA
Captain Phasma NA NA NA NA NA NA NA NA NA

Here, I’ve created z-scored and centered (i.e. only subtracted the mean but didn’t divide by the standard deviation) versions of the height, mass, and birth_year columns in one go.

You can use the everything() helper function if you want to apply a function to all of the columns in your data frame.

df.starwars %>% 
  select(height, mass) %>%
  mutate(across(.cols = everything(),
                .fns = as.character)) # transform all columns to characters
height mass
172 77
167 75
96 32
202 136
150 49
178 120
165 75
97 32
183 84
182 77
188 84
180 NA
228 112
180 80
173 74
175 1358
170 77
180 110
66 17
170 75
183 78.2
200 140
190 113
177 79
175 79
180 83
150 NA
NA NA
88 20
160 68
193 89
191 90
170 NA
185 45
196 66
224 82
206 NA
183 NA
137 NA
112 40
183 NA
163 NA
175 80
180 NA
178 55
79 15
94 45
122 NA
163 65
188 84
198 82
196 87
171 NA
184 50
188 NA
264 NA
188 80
196 NA
185 85
157 NA
183 NA
183 80
170 56.2
166 50
165 NA
193 80
191 NA
183 79
168 55
198 102
229 88
213 NA
167 NA
96 NA
193 48
191 NA
178 57
216 159
234 136
188 79
178 48
206 80
NA NA
NA NA
NA NA
NA NA
NA NA

Here, I’ve selected some columns first, and then changed the mode to character in each of them.

Sometimes, you want to apply a function only to those columns that have a particular data type. This is where where() comes in handy!

For example, the following code changes all the numeric columns to character columns:

df.starwars %>% 
  mutate(across(.cols = where(~ is.numeric(.)),
                .fns = ~ as.character(.)))
name height mass hair_color skin_color eye_color birth_year sex gender homeworld species films vehicles starships
Luke Skywalker 172 77 blond fair blue 19 male masculine Tatooine Human A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith , The Force Awakens Snowspeeder , Imperial Speeder Bike X-wing , Imperial shuttle
C-3PO 167 75 NA gold yellow 112 none masculine Tatooine Droid A New Hope , The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith
R2-D2 96 32 NA white, blue red 33 none masculine Naboo Droid A New Hope , The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith , The Force Awakens
Darth Vader 202 136 none white yellow 41.9 male masculine Tatooine Human A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith TIE Advanced x1
Leia Organa 150 49 brown light brown 19 female feminine Alderaan Human A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith , The Force Awakens Imperial Speeder Bike
Owen Lars 178 120 brown, grey light blue 52 male masculine Tatooine Human A New Hope , Attack of the Clones, Revenge of the Sith
Beru Whitesun Lars 165 75 brown light blue 47 female feminine Tatooine Human A New Hope , Attack of the Clones, Revenge of the Sith
R5-D4 97 32 NA white, red red NA none masculine Tatooine Droid A New Hope
Biggs Darklighter 183 84 black light brown 24 male masculine Tatooine Human A New Hope X-wing
Obi-Wan Kenobi 182 77 auburn, white fair blue-gray 57 male masculine Stewjon Human A New Hope , The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith Tribubble bongo Jedi starfighter , Trade Federation cruiser, Naboo star skiff , Jedi Interceptor , Belbullab-22 starfighter
Anakin Skywalker 188 84 blond fair blue 41.9 male masculine Tatooine Human The Phantom Menace , Attack of the Clones, Revenge of the Sith Zephyr-G swoop bike, XJ-6 airspeeder Naboo fighter , Trade Federation cruiser, Jedi Interceptor
Wilhuff Tarkin 180 NA auburn, grey fair blue 64 male masculine Eriadu Human A New Hope , Revenge of the Sith
Chewbacca 228 112 brown unknown blue 200 male masculine Kashyyyk Wookiee A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith , The Force Awakens AT-ST Millennium Falcon, Imperial shuttle
Han Solo 180 80 brown fair brown 29 male masculine Corellia Human A New Hope , The Empire Strikes Back, Return of the Jedi , The Force Awakens Millennium Falcon, Imperial shuttle
Greedo 173 74 NA green black 44 male masculine Rodia Rodian A New Hope
Jabba Desilijic Tiure 175 1358 NA green-tan, brown orange 600 hermaphroditic masculine Nal Hutta Hutt A New Hope , Return of the Jedi, The Phantom Menace
Wedge Antilles 170 77 brown fair hazel 21 male masculine Corellia Human A New Hope , The Empire Strikes Back, Return of the Jedi Snowspeeder X-wing
Jek Tono Porkins 180 110 brown fair blue NA NA NA Bestine IV NA A New Hope X-wing
Yoda 66 17 white green brown 896 male masculine NA Yoda’s species The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith
Palpatine 170 75 grey pale yellow 82 male masculine Naboo Human The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith
Boba Fett 183 78.2 black fair brown 31.5 male masculine Kamino Human The Empire Strikes Back, Return of the Jedi , Attack of the Clones Slave 1
IG-88 200 140 none metal red 15 none masculine NA Droid The Empire Strikes Back
Bossk 190 113 none green red 53 male masculine Trandosha Trandoshan The Empire Strikes Back
Lando Calrissian 177 79 black dark brown 31 male masculine Socorro Human The Empire Strikes Back, Return of the Jedi Millennium Falcon
Lobot 175 79 none light blue 37 male masculine Bespin Human The Empire Strikes Back
Ackbar 180 83 none brown mottle orange 41 male masculine Mon Cala Mon Calamari Return of the Jedi, The Force Awakens
Mon Mothma 150 NA auburn fair blue 48 female feminine Chandrila Human Return of the Jedi
Arvel Crynyd NA NA brown fair brown NA male masculine NA Human Return of the Jedi A-wing
Wicket Systri Warrick 88 20 brown brown brown 8 male masculine Endor Ewok Return of the Jedi
Nien Nunb 160 68 none grey black NA male masculine Sullust Sullustan Return of the Jedi Millennium Falcon
Qui-Gon Jinn 193 89 brown fair blue 92 male masculine NA Human The Phantom Menace Tribubble bongo
Nute Gunray 191 90 none mottled green red NA male masculine Cato Neimoidia Neimodian The Phantom Menace , Attack of the Clones, Revenge of the Sith
Finis Valorum 170 NA blond fair blue 91 male masculine Coruscant Human The Phantom Menace
Padmé Amidala 185 45 brown light brown 46 female feminine Naboo Human The Phantom Menace , Attack of the Clones, Revenge of the Sith Naboo fighter , H-type Nubian yacht, Naboo star skiff
Jar Jar Binks 196 66 none orange orange 52 male masculine Naboo Gungan The Phantom Menace , Attack of the Clones
Roos Tarpals 224 82 none grey orange NA male masculine Naboo Gungan The Phantom Menace
Rugor Nass 206 NA none green orange NA male masculine Naboo Gungan The Phantom Menace
Ric Olié 183 NA brown fair blue NA male masculine Naboo Human The Phantom Menace Naboo Royal Starship
Watto 137 NA black blue, grey yellow NA male masculine Toydaria Toydarian The Phantom Menace , Attack of the Clones
Sebulba 112 40 none grey, red orange NA male masculine Malastare Dug The Phantom Menace
Quarsh Panaka 183 NA black dark brown 62 male masculine Naboo Human The Phantom Menace
Shmi Skywalker 163 NA black fair brown 72 female feminine Tatooine Human The Phantom Menace , Attack of the Clones
Darth Maul 175 80 none red yellow 54 male masculine Dathomir Zabrak The Phantom Menace Sith speeder Scimitar
Bib Fortuna 180 NA none pale pink NA male masculine Ryloth Twi’lek Return of the Jedi
Ayla Secura 178 55 none blue hazel 48 female feminine Ryloth Twi’lek The Phantom Menace , Attack of the Clones, Revenge of the Sith
Ratts Tyerel 79 15 none grey, blue unknown NA male masculine Aleen Minor Aleena The Phantom Menace
Dud Bolt 94 45 none blue, grey yellow NA male masculine Vulpter Vulptereen The Phantom Menace
Gasgano 122 NA none white, blue black NA male masculine Troiken Xexto The Phantom Menace
Ben Quadinaros 163 65 none grey, green, yellow orange NA male masculine Tund Toong The Phantom Menace
Mace Windu 188 84 none dark brown 72 male masculine Haruun Kal Human The Phantom Menace , Attack of the Clones, Revenge of the Sith
Ki-Adi-Mundi 198 82 white pale yellow 92 male masculine Cerea Cerean The Phantom Menace , Attack of the Clones, Revenge of the Sith
Kit Fisto 196 87 none green black NA male masculine Glee Anselm Nautolan The Phantom Menace , Attack of the Clones, Revenge of the Sith
Eeth Koth 171 NA black brown brown NA male masculine Iridonia Zabrak The Phantom Menace , Revenge of the Sith
Adi Gallia 184 50 none dark blue NA female feminine Coruscant Tholothian The Phantom Menace , Revenge of the Sith
Saesee Tiin 188 NA none pale orange NA male masculine Iktotch Iktotchi The Phantom Menace , Revenge of the Sith
Yarael Poof 264 NA none white yellow NA male masculine Quermia Quermian The Phantom Menace
Plo Koon 188 80 none orange black 22 male masculine Dorin Kel Dor The Phantom Menace , Attack of the Clones, Revenge of the Sith Jedi starfighter
Mas Amedda 196 NA none blue blue NA male masculine Champala Chagrian The Phantom Menace , Attack of the Clones
Gregar Typho 185 85 black dark brown NA NA NA Naboo NA Attack of the Clones Naboo fighter
Cordé 157 NA brown light brown NA NA NA Naboo NA Attack of the Clones
Cliegg Lars 183 NA brown fair blue 82 male masculine Tatooine Human Attack of the Clones
Poggle the Lesser 183 80 none green yellow NA male masculine Geonosis Geonosian Attack of the Clones, Revenge of the Sith
Luminara Unduli 170 56.2 black yellow blue 58 female feminine Mirial Mirialan Attack of the Clones, Revenge of the Sith
Barriss Offee 166 50 black yellow blue 40 female feminine Mirial Mirialan Attack of the Clones
Dormé 165 NA brown light brown NA female feminine Naboo Human Attack of the Clones
Dooku 193 80 white fair brown 102 male masculine Serenno Human Attack of the Clones, Revenge of the Sith Flitknot speeder
Bail Prestor Organa 191 NA black tan brown 67 male masculine Alderaan Human Attack of the Clones, Revenge of the Sith
Jango Fett 183 79 black tan brown 66 male masculine Concord Dawn Human Attack of the Clones
Zam Wesell 168 55 blonde fair, green, yellow yellow NA female feminine Zolan Clawdite Attack of the Clones Koro-2 Exodrive airspeeder
Dexter Jettster 198 102 none brown yellow NA male masculine Ojom Besalisk Attack of the Clones
Lama Su 229 88 none grey black NA male masculine Kamino Kaminoan Attack of the Clones
Taun We 213 NA none grey black NA female feminine Kamino Kaminoan Attack of the Clones
Jocasta Nu 167 NA white fair blue NA female feminine Coruscant Human Attack of the Clones
R4-P17 96 NA none silver, red red, blue NA none feminine NA Droid Attack of the Clones, Revenge of the Sith
Wat Tambor 193 48 none green, grey unknown NA male masculine Skako Skakoan Attack of the Clones
San Hill 191 NA none grey gold NA male masculine Muunilinst Muun Attack of the Clones
Shaak Ti 178 57 none red, blue, white black NA female feminine Shili Togruta Attack of the Clones, Revenge of the Sith
Grievous 216 159 none brown, white green, yellow NA male masculine Kalee Kaleesh Revenge of the Sith Tsmeu-6 personal wheel bike Belbullab-22 starfighter
Tarfful 234 136 brown brown blue NA male masculine Kashyyyk Wookiee Revenge of the Sith
Raymus Antilles 188 79 brown light brown NA male masculine Alderaan Human A New Hope , Revenge of the Sith
Sly Moore 178 48 none pale white NA NA NA Umbara NA Attack of the Clones, Revenge of the Sith
Tion Medon 206 80 none grey black NA male masculine Utapau Pau’an Revenge of the Sith
Finn NA NA black dark dark NA male masculine NA Human The Force Awakens
Rey NA NA brown light hazel NA female feminine NA Human The Force Awakens
Poe Dameron NA NA brown light brown NA male masculine NA Human The Force Awakens X-wing
BB8 NA NA none none black NA none masculine NA Droid The Force Awakens
Captain Phasma NA NA none none unknown NA female feminine NA Human The Force Awakens

Or we could round all the numeric columns to one digit:

df.starwars %>% 
  mutate(across(.cols = where(~ is.numeric(.)),
                .fns = ~ round(., digits = 1)))
name height mass hair_color skin_color eye_color birth_year sex gender homeworld species films vehicles starships
Luke Skywalker 172 77.0 blond fair blue 19.0 male masculine Tatooine Human A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith , The Force Awakens Snowspeeder , Imperial Speeder Bike X-wing , Imperial shuttle
C-3PO 167 75.0 NA gold yellow 112.0 none masculine Tatooine Droid A New Hope , The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith
R2-D2 96 32.0 NA white, blue red 33.0 none masculine Naboo Droid A New Hope , The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith , The Force Awakens
Darth Vader 202 136.0 none white yellow 41.9 male masculine Tatooine Human A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith TIE Advanced x1
Leia Organa 150 49.0 brown light brown 19.0 female feminine Alderaan Human A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith , The Force Awakens Imperial Speeder Bike
Owen Lars 178 120.0 brown, grey light blue 52.0 male masculine Tatooine Human A New Hope , Attack of the Clones, Revenge of the Sith
Beru Whitesun Lars 165 75.0 brown light blue 47.0 female feminine Tatooine Human A New Hope , Attack of the Clones, Revenge of the Sith
R5-D4 97 32.0 NA white, red red NA none masculine Tatooine Droid A New Hope
Biggs Darklighter 183 84.0 black light brown 24.0 male masculine Tatooine Human A New Hope X-wing
Obi-Wan Kenobi 182 77.0 auburn, white fair blue-gray 57.0 male masculine Stewjon Human A New Hope , The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith Tribubble bongo Jedi starfighter , Trade Federation cruiser, Naboo star skiff , Jedi Interceptor , Belbullab-22 starfighter
Anakin Skywalker 188 84.0 blond fair blue 41.9 male masculine Tatooine Human The Phantom Menace , Attack of the Clones, Revenge of the Sith Zephyr-G swoop bike, XJ-6 airspeeder Naboo fighter , Trade Federation cruiser, Jedi Interceptor
Wilhuff Tarkin 180 NA auburn, grey fair blue 64.0 male masculine Eriadu Human A New Hope , Revenge of the Sith
Chewbacca 228 112.0 brown unknown blue 200.0 male masculine Kashyyyk Wookiee A New Hope , The Empire Strikes Back, Return of the Jedi , Revenge of the Sith , The Force Awakens AT-ST Millennium Falcon, Imperial shuttle
Han Solo 180 80.0 brown fair brown 29.0 male masculine Corellia Human A New Hope , The Empire Strikes Back, Return of the Jedi , The Force Awakens Millennium Falcon, Imperial shuttle
Greedo 173 74.0 NA green black 44.0 male masculine Rodia Rodian A New Hope
Jabba Desilijic Tiure 175 1358.0 NA green-tan, brown orange 600.0 hermaphroditic masculine Nal Hutta Hutt A New Hope , Return of the Jedi, The Phantom Menace
Wedge Antilles 170 77.0 brown fair hazel 21.0 male masculine Corellia Human A New Hope , The Empire Strikes Back, Return of the Jedi Snowspeeder X-wing
Jek Tono Porkins 180 110.0 brown fair blue NA NA NA Bestine IV NA A New Hope X-wing
Yoda 66 17.0 white green brown 896.0 male masculine NA Yoda’s species The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith
Palpatine 170 75.0 grey pale yellow 82.0 male masculine Naboo Human The Empire Strikes Back, Return of the Jedi , The Phantom Menace , Attack of the Clones , Revenge of the Sith
Boba Fett 183 78.2 black fair brown 31.5 male masculine Kamino Human The Empire Strikes Back, Return of the Jedi , Attack of the Clones Slave 1
IG-88 200 140.0 none metal red 15.0 none masculine NA Droid The Empire Strikes Back
Bossk 190 113.0 none green red 53.0 male masculine Trandosha Trandoshan The Empire Strikes Back
Lando Calrissian 177 79.0 black dark brown 31.0 male masculine Socorro Human The Empire Strikes Back, Return of the Jedi Millennium Falcon
Lobot 175 79.0 none light blue 37.0 male masculine Bespin Human The Empire Strikes Back
Ackbar 180 83.0 none brown mottle orange 41.0 male masculine Mon Cala Mon Calamari Return of the Jedi, The Force Awakens
Mon Mothma 150 NA auburn fair blue 48.0 female feminine Chandrila Human Return of the Jedi
Arvel Crynyd NA NA brown fair brown NA male masculine NA Human Return of the Jedi A-wing
Wicket Systri Warrick 88 20.0 brown brown brown 8.0 male masculine Endor Ewok Return of the Jedi
Nien Nunb 160 68.0 none grey black NA male masculine Sullust Sullustan Return of the Jedi Millennium Falcon
Qui-Gon Jinn 193 89.0 brown fair blue 92.0 male masculine NA Human The Phantom Menace Tribubble bongo
Nute Gunray 191 90.0 none mottled green red NA male masculine Cato Neimoidia Neimodian The Phantom Menace , Attack of the Clones, Revenge of the Sith
Finis Valorum 170 NA blond fair blue 91.0 male masculine Coruscant Human The Phantom Menace
Padmé Amidala 185 45.0 brown light brown 46.0 female feminine Naboo Human The Phantom Menace , Attack of the Clones, Revenge of the Sith Naboo fighter , H-type Nubian yacht, Naboo star skiff
Jar Jar Binks 196 66.0 none orange orange 52.0 male masculine Naboo Gungan The Phantom Menace , Attack of the Clones
Roos Tarpals 224 82.0 none grey orange NA male masculine Naboo Gungan The Phantom Menace
Rugor Nass 206 NA none green orange NA male masculine Naboo Gungan The Phantom Menace
Ric Olié 183 NA brown fair blue NA male masculine Naboo Human The Phantom Menace Naboo Royal Starship
Watto 137 NA black blue, grey yellow NA male masculine Toydaria Toydarian The Phantom Menace , Attack of the Clones
Sebulba 112 40.0 none grey, red orange NA male masculine Malastare Dug The Phantom Menace
Quarsh Panaka 183 NA black dark brown 62.0 male masculine Naboo Human The Phantom Menace
Shmi Skywalker 163 NA black fair brown 72.0 female feminine Tatooine Human The Phantom Menace , Attack of the Clones
Darth Maul 175 80.0 none red yellow 54.0 male masculine Dathomir Zabrak The Phantom Menace Sith speeder Scimitar
Bib Fortuna 180 NA none pale pink NA male masculine Ryloth Twi’lek Return of the Jedi
Ayla Secura 178 55.0 none blue hazel 48.0 female feminine Ryloth Twi’lek The Phantom Menace , Attack of the Clones, Revenge of the Sith
Ratts Tyerel 79 15.0 none grey, blue unknown NA male masculine Aleen Minor Aleena The Phantom Menace
Dud Bolt 94 45.0 none blue, grey yellow NA male masculine Vulpter Vulptereen The Phantom Menace
Gasgano 122 NA none white, blue black NA male masculine Troiken Xexto The Phantom Menace
Ben Quadinaros 163 65.0 none grey, green, yellow orange NA male masculine Tund Toong The Phantom Menace
Mace Windu 188 84.0 none dark brown 72.0 male masculine Haruun Kal Human The Phantom Menace , Attack of the Clones, Revenge of the Sith
Ki-Adi-Mundi 198 82.0 white pale yellow 92.0 male masculine Cerea Cerean The Phantom Menace , Attack of the Clones, Revenge of the Sith
Kit Fisto 196 87.0 none green black NA male masculine Glee Anselm Nautolan The Phantom Menace , Attack of the Clones, Revenge of the Sith
Eeth Koth 171 NA black brown brown NA male masculine Iridonia Zabrak The Phantom Menace , Revenge of the Sith
Adi Gallia 184 50.0 none dark blue NA female feminine Coruscant Tholothian The Phantom Menace , Revenge of the Sith
Saesee Tiin 188 NA none pale orange NA male masculine Iktotch Iktotchi The Phantom Menace , Revenge of the Sith
Yarael Poof 264 NA none white yellow NA male masculine Quermia Quermian The Phantom Menace
Plo Koon 188 80.0 none orange black 22.0 male masculine Dorin Kel Dor The Phantom Menace , Attack of the Clones, Revenge of the Sith Jedi starfighter
Mas Amedda 196 NA none blue blue NA male masculine Champala Chagrian The Phantom Menace , Attack of the Clones
Gregar Typho 185 85.0 black dark brown NA NA NA Naboo NA Attack of the Clones Naboo fighter
Cordé 157 NA brown light brown NA NA NA Naboo NA Attack of the Clones
Cliegg Lars 183 NA brown fair blue 82.0 male masculine Tatooine Human Attack of the Clones
Poggle the Lesser 183 80.0 none green yellow NA male masculine Geonosis Geonosian Attack of the Clones, Revenge of the Sith
Luminara Unduli 170 56.2 black yellow blue 58.0 female feminine Mirial Mirialan Attack of the Clones, Revenge of the Sith
Barriss Offee 166 50.0 black yellow blue 40.0 female feminine Mirial Mirialan Attack of the Clones
Dormé 165 NA brown light brown NA female feminine Naboo Human Attack of the Clones
Dooku 193 80.0 white fair brown 102.0 male masculine Serenno Human Attack of the Clones, Revenge of the Sith Flitknot speeder
Bail Prestor Organa 191 NA black tan brown 67.0 male masculine Alderaan Human Attack of the Clones, Revenge of the Sith
Jango Fett 183 79.0 black tan brown 66.0 male masculine Concord Dawn Human Attack of the Clones
Zam Wesell 168 55.0 blonde fair, green, yellow yellow NA female feminine Zolan Clawdite Attack of the Clones Koro-2 Exodrive airspeeder
Dexter Jettster 198 102.0 none brown yellow NA male masculine Ojom Besalisk Attack of the Clones
Lama Su 229 88.0 none grey black NA male masculine Kamino Kaminoan Attack of the Clones
Taun We 213 NA none grey black NA female feminine Kamino Kaminoan Attack of the Clones
Jocasta Nu 167 NA white fair blue NA female feminine Coruscant Human Attack of the Clones
R4-P17 96 NA none silver, red red, blue NA none feminine NA Droid Attack of the Clones, Revenge of the Sith
Wat Tambor 193 48.0 none green, grey unknown NA male masculine Skako Skakoan Attack of the Clones
San Hill 191 NA none grey gold NA male masculine Muunilinst Muun Attack of the Clones
Shaak Ti 178 57.0 none red, blue, white black NA female feminine Shili Togruta Attack of the Clones, Revenge of the Sith
Grievous 216 159.0 none brown, white green, yellow NA male masculine Kalee Kaleesh Revenge of the Sith Tsmeu-6 personal wheel bike Belbullab-22 starfighter
Tarfful 234 136.0 brown brown blue NA male masculine Kashyyyk Wookiee Revenge of the Sith
Raymus Antilles 188 79.0 brown light brown NA male masculine Alderaan Human A New Hope , Revenge of the Sith
Sly Moore 178 48.0 none pale white NA NA NA Umbara NA Attack of the Clones, Revenge of the Sith
Tion Medon 206 80.0 none grey black NA male masculine Utapau Pau’an Revenge of the Sith
Finn NA NA black dark dark NA male masculine NA Human The Force Awakens
Rey NA NA brown light hazel NA female feminine NA Human The Force Awakens
Poe Dameron NA NA brown light brown NA male masculine NA Human The Force Awakens X-wing
BB8 NA NA none none black NA none masculine NA Droid The Force Awakens
Captain Phasma NA NA none none unknown NA female feminine NA Human The Force Awakens

Practice 3

Compute the body mass index for masculine characters who are human.

  • select only the columns you need
  • filter out only the rows you need
  • make the new variable with the body mass index
  • arrange the data frame starting with the highest body mass index
# write your code here 

Additional resources

Cheatsheets

  • base R –> summary of how to use base R (we will mostly use the tidyverse but it’s still important to know how to do things in base R)
  • data transformation –> transforming data using dplyr

Data camp courses

Books and chapters

Session info

Information about this R session including which version of R was used, and what packages were loaded.

sessionInfo()
R version 4.4.3 (2025-02-28)
Platform: aarch64-apple-darwin20.0.0
Running under: macOS Sonoma 14.7

Matrix products: default
BLAS/LAPACK: /Users/esh/Dropbox/docs/teaching/fundamentals-workshop/.pixi/envs/default/lib/libopenblas.0.dylib;  LAPACK version 3.12.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: America/Los_Angeles
tzcode source: system (macOS)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] lubridate_1.9.4 forcats_1.0.0   stringr_1.5.2   dplyr_1.1.4    
 [5] purrr_1.1.0     readr_2.1.5     tidyr_1.3.1     tibble_3.3.0   
 [9] ggplot2_4.0.0   tidyverse_2.0.0 DT_0.34.0       visdat_0.6.0   
[13] skimr_2.2.1     knitr_1.50     

loaded via a namespace (and not attached):
 [1] gtable_0.3.6       jsonlite_2.0.0     compiler_4.4.3     tidyselect_1.2.1  
 [5] jquerylib_0.1.4    scales_1.4.0       yaml_2.3.10        fastmap_1.2.0     
 [9] R6_2.6.1           labeling_0.4.3     generics_0.1.4     htmlwidgets_1.6.4 
[13] bslib_0.9.0        tzdb_0.5.0         pillar_1.11.1      RColorBrewer_1.1-3
[17] rlang_1.1.6        cachem_1.1.0       stringi_1.8.7      repr_1.1.7        
[21] xfun_0.53          sass_0.4.10        S7_0.2.0           timechange_0.3.0  
[25] cli_3.6.5          withr_3.0.2        magrittr_2.0.4     crosstalk_1.2.2   
[29] digest_0.6.37      grid_4.4.3         hms_1.1.3          base64enc_0.1-3   
[33] lifecycle_1.0.4    vctrs_0.6.5        evaluate_1.0.5     glue_1.8.0        
[37] farver_2.1.2       codetools_0.2-20   rmarkdown_2.29     tools_4.4.3       
[41] pkgconfig_2.0.3    htmltools_0.5.8.1 
Back to top