3.3 A Pipe Aside
If you were typing out this code, you’d be very tired of typing the dataset name by now. Enter the %$% pipe from the magrittr package. Remember how we said we access columns from a dataset using the dollar sign? This pipe puts whatever is on the left of the pipe in front of whatever is on the right of the pipe, with a dollar sign in between.
Convince yourself that the pipe does this with the below code. You don’t have to use this pipe, but it can make using tidyverse functions and base R together much easier, and gives you a convenient syntax for always starting with the dataset as the leftmost argument.
If you want to know more about the describe function, you can get help by typing ?describe into the Console. This is true for any function in R.
#--- Get a detailed summary
describe(sdg$tb)
## vars n mean sd median trimmed mad min max range skew kurtosis
## X1 1 210 110.39 149.66 50.5 78.43 63.9 0 834 834 2.06 4.61
## se
## X1 10.33
#--- Same thing, but with a pipe
sdg %$% describe(tb)
## vars n mean sd median trimmed mad min max range skew kurtosis
## X1 1 210 110.39 149.66 50.5 78.43 63.9 0 834 834 2.06 4.61
## se
## X1 10.33
#--- Get the correlation between TB and Gini coefficient
## Note that the 'use' argument says to only use data where both observations are not missing
cor(sdg$tb, sdg$gini, use = "complete.obs")
## [1] 0.3585926
## This correlation says that TB & ARI are slightly positively associated.
## As income inequality goes up, so does TB (this shouldn't be surprising!)
#--- The same code with a pipe
sdg %$% cor(tb, gini, use = "complete.obs")
## [1] 0.3585926