One of my most favorite functions in [R] is dplyr::mutate()
. The dplyr package is a very useful package with functions designed for data manipulation operations. It is also interesting that its function names are verb terms, which makes it easier for the programmers to understand what they do. Example of such functions are filter()
, arrange()
, select()
, summarise()
, and mutate()
. In this post, we focus on mutate()
.
We start with the dataframe df
that has two columns firstcol
and secondcol
. If we want to add two new columns, say newcol
and kilonewcol
where newcol = firstcol + secondcol
and kilonewcol = newcol / 1000
, we just call the dplyr::mutate()
function to perform the operation for us.
library("dplyr") mutate(df, newcol = firstcol + secondcol, kilonewcol = newcol / 1000)
If you don’t have the package, install it using
install.packages("dplyr")