Skip to content
R for the Rest of Us Logo

This lesson is locked

Get access to all lessons in this course.

Transcript

Click on the transcript to go to that point in the video. Please note that transcripts are auto generated and may contain minor inaccuracies.

View code shown in video
# Load Packages -----------------------------------------------------------

library(tidyverse)

# Import Data -------------------------------------------------------------

penguins <- read_csv("penguins.csv")

# mutate() ----------------------------------------------------------------

# We use mutate() we make new variables or change existing ones. 

# We can use mutate() in three ways. 

# 1. Create a new variable with a specific value:

penguins |> 
  mutate(continent = "Antarctica")

# 2. Create a new variable based on other variables:

penguins |> 
  mutate(body_mass_lbs = body_mass_g / 453.6)

# 3. Change an existing variable

penguins |>
  mutate(bill_length_mm = bill_length_mm + 1)

Your Turn

# Load Packages -----------------------------------------------------------

# Load the tidyverse package

library(tidyverse)

# Import Data -------------------------------------------------------------

# Download data from https://rfor.us/penguins
# Copy the data into the RStudio project
# Create a new R script file and add code to import your data

penguins <- read_csv("penguins.csv")
			
# mutate() ----------------------------------------------------------------

# Use mutate() to create a variable called observation_station and set its value to "Palmer"

# YOUR CODE HERE

# 2. Create a new variable based on other variables:

# YOUR CODE HERE

# 3. Change an existing variable

# YOUR CODE HERE

Learn More

To learn more about the mutate() function, check out Chapter 3 of R for Data Science.

Have any questions? Put them below and we will help you out!

You need to be signed-in to comment on this post. Login.

There are some things I'd like to do for part 3 of the assignment (change an existing variable) that I don't know how to do. For example, I'd like to change the "sex" variable to be called "female" and then change the values from female-> 1, male -> 0, and NA -> NA. How can I find the functions that will enable me to do that?

Libby Heeren

Libby Heeren

October 14, 2023

Hey, Rob! The case_when() function will allow you to change values from female-> 1, male -> 0, and NA -> NA and the rename() function will allow you to rename a column. You'll get to these functions in time throughout the course once you get to the advanced data wrangling sections, but if you'd like to check out an older video clip (videos are in the process of being replaced) you can see one about case_when here.

If you'd like to try out the rename() function, try adding a line to your dplyr code like this: penguins |> mutate(continent = "Antarctica") |> rename(sex_numeric = sex_v2)

The syntax works like this here: rename(new_column_name = old_column_name)

Please feel free to message me on Discord with any questions!