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. |
Contact Us