#> [1] 0.9645593 2.4985323 3.6130830 3.4038356 6.6371189 3.9822256
#> [7] 7.6365972 8.9805052 12.6891921 7.6383656
Anonymous Functions
Pipes
Scripting
Data Manipulation
An anonymous function is a function that is not stored in an R object for the global environment. It can be thought of as a temporary function to complete a task. A common way to used an anonymous function is with an *apply()
function
#> [1] 0.9645593 2.4985323 3.6130830 3.4038356 6.6371189 3.9822256
#> [7] 7.6365972 8.9805052 12.6891921 7.6383656
Use an anonymous function to square all the values in the following vector:
Use an anonymous function to convert the vector from Fahrenheit to Celsius:
\[ C = \frac{5(F-32)}{9} \]
Pipes are used to pass the output from one function and use it as input for another function. The output is piped into the first argument of the next function. There are two main pipes: R’s base pipe and Magrittr’s pipes. You must download and install the magrittr
package; and you will need to load it everytime:
Additionally, pipes can be used to chain functions together.
|>
Before R 4.1, R did not have a pipe in its main program. The base pipe, |>
, will pipe the output of the first operation and use it as the input of the first argument of the next function.
%>%
The magrittr pipe, %>%
, operates the same way as |>
. Below are a couple of examples
%$%
The exposition pipe, %$%
, will expose the named elements, from a list or data frame, to the next function.
%T>%
The Tee pipe, %T>%
, forward the output in the
%T>%
Using the vector below, find the standard deviation using a pipe:
Chain pipe the previous results into the \(sin(x)\).
Chain pipe the previous results into \(e^x\).
Chain pipe the previous results into \(x^2+5x+4\)
The structure a programming is important to ensure that all methods are executed properly.
Below is a list of recommended keyboard shortcuts:
Shortcut | Windows/Linux | Mac |
---|---|---|
%>% |
Ctrl+Shift+M | Cmd+Shift+M |
Run Current Line | Ctrl+Enter | Cmd+Return |
Run Current Chunk | Ctrl+Shift+Enter | Cmd+Shift+Enter |
Knit Document | Ctrl+Shift+K | Cmd+Shift+K |
Add Cursor Below | Ctrl+Alt+Down | Cmd+Alt+Down |
Comment Line | Ctrl+Shift+C | Cmd+Shift+C |
I recommend modify these keyboard shortcuts in RStudio
Shortcut | Windows/Linux | Mac |
---|---|---|
%in% |
Ctrl+Shift+I | Cmd+Shift+I |
%$% |
Ctrl+Shift+D | Cmd+Shift+D |
%T>% |
Ctrl+Shift+T | Cmd+Shift+T |
Note you will need to install the extraInserts
package:
Tidyverse is a collection of R packages used for data manipulation. The dplyr
package is known as the grammar of data manipulation with a set
mutate()
adds new variablesselect()
selects variablesfilter()
filters dataif_else()
conditional function that returns 2 valuesgroup_by()
a dataset is grouped by factorssummarise()
provides summaries of datalibrary(palmerpenguins)
sum_stats <- penguins %>%
drop_na %>%
filter(year==2007) %>%
group_by(island) %>%
summarise(mean = mean(bill_length_mm),
sd = sd(bill_length_mm),
median = median(bill_length_mm),
n = length(bill_length_mm)) %>%
print
#> # A tibble: 3 × 5
#> island mean sd median n
#> <fct> <dbl> <dbl> <dbl> <int>
#> 1 Biscoe 45.1 4.80 46.1 43
#> 2 Dream 44.7 5.64 45.4 45
#> 3 Torgersen 39.0 2.92 39.1 15