How to Use Mutate function in R (2024)

[This article was first published on Data Science Tutorials, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post How to Use Mutate function in R appeared first on Data Science Tutorials

How to Use Mutate function in R, This article demonstrates how to add additional variables to a data frame using R’s mutate() function.

Artificial Intelligence Examples-Quick View – Data Science Tutorials

How to Use Mutate function in R

The dplyr library has the following functions that can be used to add additional variables to a data frame.

mutate() – adds new variables while retaining old variables to a data frame.

transmute() – adds new variables and removes old ones from a data frame.

mutate_all() – changes every variable in a data frame simultaneously.

mutate_at() – changes certain variables by name.

mutate_if() – alterations all variables that satisfy a specific criterion

Tips for Rearranging Columns in R – Data Science Tutorials

mutate()

A data frame’s existing variables are preserved when new variables are added using the mutate() function. The mutate() basic syntax is as follows.

data <- mutate(new_variable = existing_variable/3)

data: the fresh data frame where the fresh variables will be placed

new_variable: the name of the new variable

existing_variable: the current data frame variable that you want to modify in order to generate a new variable

As an illustration, the code that follows shows how to modify the built-in iris dataset to include a new variable called root sepal width.

glm function in r-Generalized Linear Models – Data Science Tutorials

The first six lines of the iris dataset should be defined as a data frame.

data <- head(iris)dataSepal.Length Sepal.Width Petal.Length Petal.Width Species1 5.1 3.5 1.4 0.2 setosa2 4.9 3.0 1.4 0.2 setosa3 4.7 3.2 1.3 0.2 setosa4 4.6 3.1 1.5 0.2 setosa5 5.0 3.6 1.4 0.2 setosa6 5.4 3.9 1.7 0.4 setosalibrary(dplyr)

Set the new column’s root sepal width to the sepal’s square root. variable width

How to perform the MANOVA test in R? – Data Science Tutorials

data %>% mutate(root_sepal_width = sqrt(Sepal.Width)) Sepal.Length Sepal.Width Petal.Length Petal.Width Species root_sepal_width1 5.1 3.5 1.4 0.2 setosa 1.8708292 4.9 3.0 1.4 0.2 setosa 1.7320513 4.7 3.2 1.3 0.2 setosa 1.7888544 4.6 3.1 1.5 0.2 setosa 1.7606825 5.0 3.6 1.4 0.2 setosa 1.8973676 5.4 3.9 1.7 0.4 setosa 1.974842

transmute()

A data frame’s variables are added and removed via the transmute() method. The code that follows demonstrates how to eliminate all of the existing variables and add two new variables to a dataset.

Checking Missing Values in R – Data Science Tutorials

The first six lines of the iris dataset should be defined as a data frame.

data <- head(iris)data Sepal.Length Sepal.Width Petal.Length Petal.Width Species1 5.1 3.5 1.4 0.2 setosa2 4.9 3.0 1.4 0.2 setosa3 4.7 3.2 1.3 0.2 setosa4 4.6 3.1 1.5 0.2 setosa5 5.0 3.6 1.4 0.2 setosa6 5.4 3.9 1.7 0.4 setosa

Create two new variables, then get rid of all the others.

Calculate the p-Value from Z-Score in R – Data Science Tutorials

data %>% transmute(root_sepal_width = sqrt(Sepal.Width), root_petal_width = sqrt(Petal.Width)) root_sepal_width root_petal_width1 1.870829 0.44721362 1.732051 0.44721363 1.788854 0.44721364 1.760682 0.44721365 1.897367 0.44721366 1.974842 0.6324555

mutate_all()

The mutate_all() function changes every variable in a data frame at once, enabling you to use the funs() function to apply a certain function to every variable.

The use of mutate_all() to divide each column in a data frame by ten is demonstrated in the code below.

Augmented Dickey-Fuller Test in R – Data Science Tutorials

The first six rows of iris sans the Species variable as the new data frame.

data2 <- head(iris) %>% select(-Species)data2

divide 10 from each of the data frame’s variables.

data2 %>% mutate_all(funs(./10))Sepal.Length Sepal.Width Petal.Length Petal.Width1 0.51 0.35 0.14 0.022 0.49 0.30 0.14 0.023 0.47 0.32 0.13 0.024 0.46 0.31 0.15 0.025 0.50 0.36 0.14 0.026 0.54 0.39 0.17 0.04

Remember that you can add more variables to the data frame by supplying a new name to be prefixed to the existing variable name.

How to Calculate Relative Frequencies in R? – Data Science Tutorials

data2 %>% mutate_all(funs(mod = ./10)) Sepal.Length Sepal.Width Petal.Length Petal.Width Sepal.Length_mod1 5.1 3.5 1.4 0.2 0.512 4.9 3.0 1.4 0.2 0.493 4.7 3.2 1.3 0.2 0.474 4.6 3.1 1.5 0.2 0.465 5.0 3.6 1.4 0.2 0.506 5.4 3.9 1.7 0.4 0.54 Sepal.Width_mod Petal.Length_mod Petal.Width_mod1 0.35 0.14 0.022 0.30 0.14 0.023 0.32 0.13 0.024 0.31 0.15 0.025 0.36 0.14 0.026 0.39 0.17 0.04

mutate_at()

Using names, the mutate at() function changes particular variables. The use of mutate_at() to divide two particular variables by 10 is demonstrated in the code below:

data2 %>% mutate_at(c("Sepal.Length", "Sepal.Width"), funs(mod = ./10))Sepal.Length Sepal.Width Petal.Length Petal.Width Sepal.Length_mod1 5.1 3.5 1.4 0.2 0.512 4.9 3.0 1.4 0.2 0.493 4.7 3.2 1.3 0.2 0.474 4.6 3.1 1.5 0.2 0.465 5.0 3.6 1.4 0.2 0.506 5.4 3.9 1.7 0.4 0.54 Sepal.Width_mod1 0.352 0.303 0.324 0.315 0.366 0.39

mutate_if()

All variables that match a specific condition are modified by the mutate_if() function.

The mutate_if() function can be used to change any variables of type factor to type character, as shown in the code below.

How to make a rounded corner bar plot in R? – Data Science Tutorials

data <- head(iris)sapply(data, class)Sepal.Length Sepal.Width Petal.Length Petal.Width Species "numeric" "numeric" "numeric" "numeric" "factor"

every factor variable can be converted to a character variable.

new_data <- data %>% mutate_if(is.factor, as.character)sapply(new_data, class)Sepal.Length Sepal.Width Petal.Length Petal.Width Species "numeric" "numeric" "numeric" "numeric" "character"

The mutate_if() method can be used to round any numeric variables to the nearest whole number using the following example code.

Calculate the P-Value from Chi-Square Statistic in R.Data Science Tutorials

In the first six rows of the iris dataset,

data <- head(iris)dataSepal.Length Sepal.Width Petal.Length Petal.Width Species1 5.1 3.5 1.4 0.2 setosa2 4.9 3.0 1.4 0.2 setosa3 4.7 3.2 1.3 0.2 setosa4 4.6 3.1 1.5 0.2 setosa5 5.0 3.6 1.4 0.2 setosa6 5.4 3.9 1.7 0.4 setosa

any numeric variables should be rounded to the nearest decimal place.

data %>% mutate_if(is.numeric, round, digits = 0)Sepal.Length Sepal.Width Petal.Length Petal.Width Species1 5 4 1 0 setosa2 5 3 1 0 setosa3 5 3 1 0 setosa4 5 3 2 0 setosa5 5 4 1 0 setosa6 5 4 2 0 setosa

The post How to Use Mutate function in R appeared first on Data Science Tutorials

Related

To leave a comment for the author, please follow the link and comment on their blog: Data Science Tutorials.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

How to Use Mutate function in R (2024)

FAQs

How to use a function in mutate R? ›

We can use the mutate() function in R programming to add new variables in the specified data frame. These new variables are added by performing the operations on present variables. Before using the mutate() function, you need to install the dplyr library. We can use the mutate() method to manipulate big datasets.

How do you mutate in R formula? ›

mutate() – adds new variables while retaining old variables to a data frame. transmute() – adds new variables and removes old ones from a data frame. mutate_all() – changes every variable in a data frame simultaneously.

Why do we use mutate function in R? ›

mutate() creates new columns that are functions of existing variables. It can also modify (if the name is the same as an existing column) and delete columns (by setting their value to NULL ).

How do I use mutate to create a new variable in R? ›

To use mutate in R, all you need to do is call the function, specify the dataframe, and specify the name-value pair for the new variable you want to create.

What does >%> mean in R? ›

%>% is called the forward pipe operator in R. It provides a mechanism for chaining commands with a new forward-pipe operator, %>%. This operator will forward a value, or the result of an expression, into the next function call/expression.

What package is needed for mutate function R? ›

Dplyr. The dplyr package is an add-on to R. It includes a host of cool functions for selecting, filtering, grouping, and arranging data. It also includes the mutate function.

How to do data transformations in R? ›

Data Transformation in R
  1. arrange() : to order the observations.
  2. select() : to select variables or columns.
  3. filter() : to filter observations by their values.
  4. gather() : to shift observations from columns to rows.
  5. spread() : to shift variables from rows to columns.
  6. group_by() & summarize() : to summarize data into groups.
Oct 23, 2020

How to change values to characters in R? ›

To convert a known factor vector to a character vector we create a duplicate copy of the input data frame. Then use the as. character() function of the R Language and pass the required factor vector column of the data frame as an argument.

How do I mutate column names in R? ›

Method 1: using colnames() method

colnames() method in R is used to rename and replace the column names of the data frame in R. The columns of the data frame can be renamed by specifying the new column names as a vector. The new name replaces the corresponding old name of the column in the data frame.

What is the difference between mutate and replace? ›

mutate() : add or change variables. Apply calculations on variables. recode() : change observations within variables. replace() : similar to recode, but you can change values using lists.

What is the difference between summarize and mutate in R? ›

It is easy to get the difference between mutate and summarize confused. Remember that mutate returns the same number of rows in a data frame, summarize returns just one row, and summarize with groups returns a row for each group.

How do I change a column value in R? ›

To replace a column value in R use square bracket notation df[] , By using this you can update values on a single column or on all columns. To refer to a single column use df$column_name . The following example updates Orange St with Portola Pkwy on the address column.

How do I calculate a new variable in R? ›

Use the assignment operator <- to create new variables. A wide array of operators and functions are available here. (To practice working with variables in R, try the first chapter of this free interactive course.)

Does mutate () adds new variables and preserves existing ones? ›

mutate() is a function you will use a lot. It is used any time you wish to create a new variable. It comes in two main flavours: mutate() and transmute(). mutate() creates a new variable and preserves the existing one, while transmute() replaces the variable.

How to replace missing variables in R? ›

You can replace NA values with zero(0) on numeric columns of R data frame by using is.na() , replace() , imputeTS::replace() , dplyr::coalesce() , dplyr::mutate_at() , dplyr::mutate_if() , and tidyr::replace_na() functions.

How do you apply a function to a vector in R? ›

Apply Function to Vector in R

To apply a function to every element of a vector in R use lapply() function. It takes a vector or list as input and always returns a list. You can use unlist() to convert list to vector in R. In returned list, each element is the result of applying FUN to the corresponding element of X .

How do you enter a function in R? ›

You can create an input function from an R data frame using the input_fn() method. You can specify feature and response variables either explicitly or using the R formula interface. Note that input_fn functions provide several parameters for controlling how data is drawn from the input source.

How to create a function in R with vectors? ›

You can create a Vector in R using c() primitive function. In R programming, the Vector contains elements of the same type and the types can be logical, integer, double, character, complex or raw. Besides c() you can also create a vector using vector(), character() functions.

Top Articles
Latest Posts
Article information

Author: Tish Haag

Last Updated:

Views: 5900

Rating: 4.7 / 5 (47 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Tish Haag

Birthday: 1999-11-18

Address: 30256 Tara Expressway, Kutchburgh, VT 92892-0078

Phone: +4215847628708

Job: Internal Consulting Engineer

Hobby: Roller skating, Roller skating, Kayaking, Flying, Graffiti, Ghost hunting, scrapbook

Introduction: My name is Tish Haag, I am a excited, delightful, curious, beautiful, agreeable, enchanting, fancy person who loves writing and wants to share my knowledge and understanding with you.