Skip to main content

The Power of dplyr in R - part 2

Let's continue our adventure with dplyr package. In the previous article I introduced select() function which select a subset of columns. Today we will focus on how to pick the observation and add a new column. We will continue using mtcars dataset which is included in your R base program.

Also I would like to say that all the posts I publish here requires basic knowledge of R and R Studio program. If you are totally new in R and don't have it installed on your computer I strongly recommend you to find some on-line tutorials and start with R fundamentals.

library(dplyr)
data("mtcars")
head(mtcars)

Now, let's introduce function:

filter()    filter a subset of rows (pick the observation) 

head(filter(.data=mtcars,mpg>20 & vs==0)) #filter rows where mpg>20 and vs= 0
          mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Mazda RX4      21   6 160.0 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag  21   6 160.0 110 3.90 2.875 17.02  0  1    4    4
Porsche 914-2  26   4 120.3  91 4.43 2.140 16.70  0  1    5    2


filter() with between() function:

filter(.data=mtcars,between(mpg,18,20)) #filter rows where values of mpg are between 18 and 2
                    mpg cyl  disp  hp drat    wt  qsec vs am gear  carb
Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
Merc 280          19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
Pontiac Firebird  19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
Ferrari Dino      19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6


slice()  select rows using their position - rows IDs (integer location) 

slice(.data = mtcars, 1:3) #choosing first three rows of the mtcars dataset

               mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4     21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710    22.8   4  108  93 3.85 2.320 18.61  1  1    4    1


mutate()  compute a new column (add a new variable while keeping the base variables) 

head(mutate(.data=mtcars,new=mpg/cyl)) #add a new variable while keeping the base variables

  mpg  cyl disp  hp  drat  wt  qsec  vs am  gear carb    new
1 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4 3.500000
2 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4 3.500000
3 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1 5.700000
4 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1 3.566667
5 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2 2.337500
6 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1 3.016667


transmute()  calculates new variables while dropping the base ones. 

head(transmute(.data=mtcars,new=mpg/cyl)) #calculates new variables while dropping the base ones.

new
1 3.500000
2 3.500000
3 5.700000
4 3.566667
5 2.337500
6 3.016667

Comments

Popular posts from this blog

Model Residuals in Time Series Data

Residuals are the indicator of the model quality. Based on Rob J Hyndman's book "Forecasting: Principles & Practice", residuals in forecasting is difference between observed value and its forecast based on all previous observations. Residuals are useful in checking whether a model has adequately captured the information in the data. All the patterns should be in the model, only randomness remains in the residuals. Therefore the ideal model has to be: uncorrelated has zero mean and useful properties are: constant variance  be normally distributed First I will activate some useful libraries we will be using. library(fpp) library(forecast) For our example I will use dowjones index as a data set. The idea will be to set up already well know simple models like: Mean Model, Naive model and Drift Model. In previous post I described  it more detailed. Next, knowing what attributes  the ideal model should  have we can check which one of those 3 are quite good or  def...

Random number generators, reproducibility and sampling with dplyr

Let's assume that you want to take some random observations from your data set. Dplyr helps you with the function sample_n(). To make your code reproducible you seed the ID of a “random” set of values. You need to indicate number of rows you want to extract and specify if the rows should be replaced or not. To show you how it works I will use again mtcars dataset which is included in your base R program. Let's see first six rows of this data frame.  library(dplyr) data("mtcars") head(mtcars)                    mpg cyl disp  hp drat    wt  qsec vs am gear carb Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4 Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4 Datsun 710        22.8   4  108  93 3.85 2.320 18.61...

The Power of dplyr in R - part 3

Today I would like to present pipe operator which simplify our code and makes it more readable. As we can see all of the dplyr functions take a data frame (or tibble) as the first argument. Dplyr provides the %>% operator from magrittr that chains the functions so x %>% f(y) turns into f(x, y). Therefore  the result from one step is then “piped” into the next step. We will use pipe operator in further examples.  Additionally we will focus on grouping, ordering and summarising functions. As previously I will continue using mtcars dataset which is included in your R base program. count() #count the unique values of one or more variables   n()  n_distinct() #number of unique observation found in a category  group_by() # group by a column, allows to group operation in the “split-apply-combine" concept   library(dplyr) data("mtcars") head(mtcars)                    mpg cyl disp  hp drat...