How it works...

The mutate() verb takes the data frame as an input, and then, it performs the operation defined within the verb. In this example, the operation was to create a new logical variable indicating whether the flight has more than 30 minutes delay in departure. Within the mutate() verb, you can define any valid new variable, and this operation can be combined with other dplyr verbs such as group_by(), filter(), select(), and so on.

The base R implementation looks pretty simple, but it takes longer than the dplyr counterpart. There is another base R alternative to create the new variable transform(). The advantage of mutate() over transform() is that you can use the newly created variable within the same mutate() verb, but it is not possible to use transform(). Here is an example of this facility:

     USAairlineData2016 <- mutate(USAairlineData2016, 
diffDepArr = (ARR_DELAY - DEP_DELAY),
diffDepArrCat = diffDepArr>0)

In the preceding code, first, the new variable diffDepArr has been created by taking the difference between the arrival delay and departure delay. Also, within the same mutate() verb, the newly created variable has been used to create another new variable. This type of operation is not possible using the transform() function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset