Mutate function in R
In this article we will discuss how we Add new Variables to a Data Frame using Existing Variables in R Programming Language using mutate() Function in R.
mutate() Function in R
mutate()
function in R Programming Language is used to add new variables in a data frame which are formed by performing operations on existing variables.
Syntax:
mutate(x, expr)
Parameters:
x: Data Frame
expr: operation on variables
Types of mutate() Function in R
In R there are five types of main function for mutate that are discribe as below. we will use
dplyr package in R
for all mutate functions.
- mutate()
- transmute()
- mutate_all()
- mutate_at()
- mutate_if()
mutate() Function in R
R
# R program to add new variables # in a data frame # Loading library library (dplyr) # Create a data frame d <- data.frame ( name = c ( "Abhi" , "Bhavesh" , "Chaman" , "Dimri" ), age = c (7, 5, 9, 16), ht = c (46, NA , NA , 69), school = c ( "yes" , "yes" , "no" , "no" ) ) # Calculating a variable x3 which is sum of height # and age printing with ht and age mutate (d, x3 = ht + age) |
Output:
name age ht school x3
1 Abhi 7 46 yes 53
2 Bhavesh 5 NA yes NA
3 Chaman 9 NA no NA
4 Dimri 16 69 no 85
transmute() Function in R
The transmute()
function in R is used to create new variables or modify existing variables in a data frame, while simultaneously dropping the variables that are not part of the result.
R
# Load the dplyr package library (dplyr) # Create a data frame d <- data.frame ( name = c ( "Abhi" , "Bhavesh" , "Chaman" , "Dimri" ), age = c (7, 5, 9, 16), ht = c (46, NA , NA , 69), school = c ( "yes" , "yes" , "no" , "no" ) ) # Use transmute to create a new variable 'age_in_months' and drop the 'age' variable result <- transmute (d, name = name, age_in_months = age * 12, ht, school) # Print the resulting data frame print (result) |
Output:
name age_in_months ht school
1 Abhi 84 46 yes
2 Bhavesh 60 NA yes
3 Chaman 108 NA no
4 Dimri 192 69 no
mutate_all() Function in R
The mutate_all()
function is used to apply a transformation to all variables in a data frame simultaneously.
R
# Load the dplyr package library (dplyr) # Create a data frame d <- data.frame ( name = c ( "Abhi" , "Bhavesh" , "Chaman" , "Dimri" ), age = c (7, 5, 9, 16), ht = c (46, NA , NA , 69), school = c ( "yes" , "yes" , "no" , "no" ) ) # Example: Double all numeric variables d_mutate_all <- d %>% mutate_all (~ ifelse ( is.numeric (.), . * 2, .)) print (d_mutate_all) |
Output:
name age ht school
1 Abhi 14 92 yes
2 Abhi 14 92 yes
3 Abhi 14 92 yes
4 Abhi 14 92 yes
mutate_at() Function in R
R
# Load the dplyr package library (dplyr) # Create a data frame d <- data.frame ( name = c ( "Abhi" , "Bhavesh" , "Chaman" , "Dimri" ), age = c (7, 5, 9, 16), ht = c (46, NA , NA , 69), school = c ( "yes" , "yes" , "no" , "no" ) ) # Example: Square only the 'age' variable d_mutate_at <- d %>% mutate_at ( vars (age), ~ .^2) print (d_mutate_at) |
Output:
name age ht school
1 Abhi 49 46 yes
2 Bhavesh 25 NA yes
3 Chaman 81 NA no
4 Dimri 256 69 no
mutate_if() Function in R
The mutate_if()
function in R, part of the dplyr
package, is used to apply a transformation to variables in a data frame based on a specific condition. It allows you to selectively apply a mutation only to the variables that satisfy the specified condition.
R
# Load the dplyr package library (dplyr) # Create a data frame d <- data.frame ( name = c ( "Abhi" , "Bhavesh" , "Chaman" , "Dimri" ), age = c (7, 5, 9, 16), ht = c (46, NA , NA , 69), school = c ( "yes" , "yes" , "no" , "no" ) ) # Use mutate_if to double the values of numeric variables d_mutate_if <- d %>% mutate_if (is.numeric, ~ . * 2) # Print the resulting data frame print (d_mutate_if) |
Output:
name age ht school
1 Abhi 14 92 yes
2 Bhavesh 10 NA yes
3 Chaman 18 NA no
4 Dimri 32 138 no
mutate() function in R
is an essential function in the data manipulation toolkit of R, providing a clean and efficient way to modify and enhance data frames.
Contact Us