Get access to all lessons in this course.
- Welcome to Fundamentals of R
- Update Everything
- Start a New Project
-
Data Wrangling and Analysis
- The Tidyverse
- Pipes
- select()
- mutate()
- filter()
- summarize()
- group_by() and summarize()
- arrange()
- Create a New Data Frame
- Bring it All Together (Data Wrangling)
-
Data Visualization
- The Grammar of Graphics
- Scatterplots
- Histograms
- Bar Charts
- Setting color and fill Aesthetic Properties
- Setting color and fill Scales
- Setting x and y Scales
- Adding Text to Plots
- Plot Labels
- Themes
- Facets
- Save Plots
- Bring it All Together (Data Visualization)
-
Quarto
- Quarto Overview
- YAML
- Text
- Code Chunks
- Tips for Working with Quarto
- Bring It All Together (Quarto)
-
Wrapping Up
- An Important Workflow Tip
Fundamentals of R
mutate()
This lesson is locked
This lesson is called mutate(), part of the Fundamentals of R course. This lesson is called mutate(), part of the Fundamentals of R course.
If the video is not playing correctly, you can watch it in a new window
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.
You need to be signed-in to comment on this post. Login.
Rob Schoen
October 13, 2023
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 Coach
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!
Gabby Bachhuber
March 19, 2024
Is it not possible to mutate categorical variable names? I tried to mutate "species", but perhaps that isn't possible (it generated an error). I think I need to use rename() instead, as noted below?
David Keyes Founder
March 19, 2024
Yes, if you just want to rename a variable (of any type),
rename()
is your best bet.Charles Obiorah
March 25, 2024
Hi David, My progress has been slowed down by how my video plays and stops. It is no longer flowing seamlessly and I wonder where I have to touch the settings. i tried another network provider and it seems to persist. Next, I tried the assignment of creating a new variable and changing an existing one. While I could see the new variable in a new column Antarctica, I could not see the new column body_mass_lbs as it is in your video. Note that I saw this on the Console:
David Keyes Founder
March 26, 2024
Sorry about the website issues. We're working to resolve these ASAP.
To answer your question, I'd need to see your code. Please paste it in the comment.
Valliappan Muthu
April 24, 2024
Hello, I have a question
Say I have a variable “plant” and which can be either “tree” or “shrub “. Coded the data in excel or spss as 1 and 0 for tree and shrub
If I want to analyse data of only “1” that is only “trees”
What should I do?
I have day where the information is coded as 1 and 0
If I want to filter these observations containing “1”
Valliappan Muthu
April 24, 2024
Hello Sorry there was an error in my code and question number 2 was resolved when I used the code Filter (plant == “1”)
I still want to know about the first question
Thanks!
Libby Heeren Coach
April 25, 2024
Hi, Valli! All of your questions can be solved using the
case_when
function insidemutate
. You can watch this video from week 7 which has an example ofcase_when
and also lists resources below the video with further examples of how to use it in different ways.An example of it in David's code during Week 7 is this:
Valliappan Muthu
April 24, 2024
Sorry I have been coming with questions from old lessons After I started cleaning the data which I work on
Kindly suggest the ways to categorise continued variable E.g say I have income of 100 individuals as a continuous variable Now I want to analyse my parameters of interest between income more than 1000$ and less than 1000$
Can I use mutate to create categories from pre existing continuous variable?
Libby Heeren Coach
April 25, 2024
See my other response about
case_when
, but you can use it to create a categorical variable based on conditional statements, such as