mutate()
function in R Language is used to add new variables in a data frame which are formed by performing operation on existing variables.
Syntax: mutate(x, expr)
Parameters:
x: Data Frame
expr: operation on variables
Example 1:
library(dplyr)
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" ) )
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
Example 2:
library(dplyr)
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" ) )
mutate(d, x3 = ht * age)
|
Output:
name age ht school x3
1 Abhi 7 46 yes 322
2 Bhavesh 5 NA yes NA
3 Chaman 9 NA no NA
4 Dimri 16 69 no 1104