mutate() & transmute() methods

These methods are used to create new variables. The mutate() function creates new variables without dropping the old ones but transmute() function drops the old variables and creates new variables. The syntax of both methods is mentioned below-

mutate(dataframeName, newVariable=formula)

transmute(dataframeName, newVariable=formula)

Example:

In this example, we created a new column avg using mutate() and transmute() methods.

R




# import dplyr package
library(dplyr)
 
# create a data frame 
stats <- data.frame(player=c('A', 'B', 'C', 'D'),
                runs=c(100, 200, 408, 19),
                wickets=c(17, 20, 7, 5))
 
# add new column avg
mutate(stats, avg=runs/4)
 
# drop all and create a new column
transmute(stats, avg=runs/4)


Output

  player runs wickets    avg
1      A  100      17  25.00
2      B  200      20  50.00
3      C  408       7 102.00
4      D   19       5   4.75
     avg
1  25.00
2  50.00
3 102.00
4   4.75

Here mutate() functions adds a new column for the existing data frame without dropping the old ones where as transmute() function created a new variable but dropped all the old columns.

Data Manipulation in R with Dplyr Package

In this article let’s discuss manipulating data in the R programming language.

In order to manipulate the data, R provides a library called dplyr which consists of many built-in methods to manipulate the data. So to use the data manipulation function, first need to import the dplyr package using library(dplyr) line of code. Below is the list of a few data manipulation functions present in dplyr package.

Function Name

Description

filter()

Produces a subset of a Data Frame.

distinct()

Removes duplicate rows in a Data Frame

arrange()

Reorder the rows of a Data Frame

select()

Produces data in required columns of a Data Frame

rename()

Renames the variable names

mutate()

Creates new variables without dropping old ones.

transmute()

Creates new variables by dropping the old.

summarize()

Gives summarized data like Average, Sum, etc.

Similar Reads

filter() method

The filter() function is used to produce the subset of the data that satisfies the condition specified in the filter() method. In the condition, we can use conditional operators, logical operators, NA values, range operators etc. to filter out data. Syntax of filter() function is given below-...

distinct() method

...

arrange() method

The distinct() method removes duplicate rows from data frame or based on the specified columns. The syntax of distinct() method is given below-...

select() method

...

rename() method

In R, the arrange() method is used to order the rows based on a specified column. The syntax of arrange() method is specified below-...

mutate() & transmute() methods

...

summarize() method

The select() method is used to extract the required columns as a table by specifying the required column names in select() method. The syntax of select() method is mentioned below-...

Contact Us