<- function(a) {
rescale <- range(a, na.rm = TRUE)
rng - rng[1])/(rng[2] - rng[1])
(a }
functions
Chapter 25
Exercise
- Write a function that takes three parameters (.data, columns, fn) that applies the mathematical function
fn
to the columns of.data
indexed numerically bycolumns
.
Apply your function to two different datasets.
Now make a summary function that allows for the possibility to remove
NA
.
- Perform a simple mathematical operation (e.g., add 1) to a chosen set of columns of a given dataset. (Use across with mutate)
- Improve your function above to allow users to input the names of the variables.
- Apply a user-defined (non-summary) function to a set of columns of data.
- Recall simple rescaling function from last week
- Apply it to
rescale
all numeric columns of a dataset. First do it with a mutate, then write a function that takesrescale
as a parameter.
my_fun(diamonds,where(is.numeric),rescale)
- For dynamically naming column names see:
https://stackoverflow.com/questions/26003574/use-dynamic-name-for-new-column-variable-in-dplyr?rq=1