A Byte of my 2.2-lb Brain

Just sharing stuff…

dplyr::mutate()

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")

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Information

This entry was posted on April 22, 2015 by in Geek and tagged , .
%d bloggers like this: