- "text": "Default Parameters\nRecall the dice-rolling example from the previous lecture. Sometimes you want to use a function frequently without re-writing the same parameters over and over again. Let’s make a more flexible function that allows us to change the number of faces on the dice being rolled.\n\n#' @details\n#' Simulates rolling `num_dice` number of dice with `n_sides` sides and outputs the sum. Note: no seed is used so the function will return a dice combination each time it is run\n#'\n#' @param num_dice integer representing number of dice to be rolled\n#' @param n_sides integer representing the number of sides of each dice\n#' @return the sum of the dice rolled\n \nroll_dice <- function(n_sides, num_dice) { \n \n # throw an error if num_dice (the input) is not an integer\n \n if(num_dice %% 1 != 0){ #if num_dice mod 1 is NOT 0\n stop(\"num_dice must be an integer\") #throw this error message and stop the function\n }\n \n #if the num_dice is an integer, continue with the function:\n sum(sample(1:n_sides, num_dice, replace=TRUE)) #sample two numbers from one to n_sides with replacement, return sum\n}\n\nNotice now that in this function, there are two parameters (the new one is n_sides). We now sample from 1:nsides (instead of 1:10) to make the function more flexible. I also renamed the function to roll_dice as we are not necessarily rolling 10 sided dice.\nSo to roll two 10-sided dice, I can call:\n\nroll_dice(n_sides = 10, num_dice = 2)\n\n[1] 9\n\n\nNow, perhaps I often want to roll ten-sided dice. To avoid having to type n_sides = 10, I can simply make the default number of sides to 10!\n\n#' @details\n#' Simulates rolling `num_dice` number of dice with `n_sides` sides and outputs the sum. Note: no seed is used so the function will return a dice combination each time it is run\n#'\n#' @param num_dice integer representing number of dice to be rolled\n#' @param n_sides integer representing the number of sides of each dice. Default is 10.\n#' @return the sum of the dice rolled\n \nroll_dice <- function(n_sides = 10, num_dice) { #NEW: n_sides default is 10\n \n # throw an error if num_dice (the input) is not an integer\n \n if(num_dice %% 1 != 0){ #if num_dice mod 1 is NOT 0\n stop(\"num_dice must be an integer\") #throw this error message and stop the function\n }\n \n #if the num_dice is an integer, continue with the function:\n sum(sample(1:n_sides, num_dice, replace=TRUE)) #sample two numbers from one to n_sides with replacement, return sum\n}\n\nWe have set n_sides = 10 as the default. This means the function will assume we have a 10 sided dice unless otherwise specified. Let’s roll 3 dice using 10 sided dice (the default):\n\nroll_dice(num_dice = 3)\n\n[1] 21\n\n\nI didn’t need to include n_sides = 10 in my function call! But I can if I want to change it to a number other than 10. Let’s roll 3 standard 6-sided dice\n\nroll_dice(n_sides = 6, num_dice = 3)\n\n[1] 9\n\n\n\n\n\n\n\n\nExercise: Default Values\n\n\n\nMake a new argument for the summarizeby_fun() we made previously called columns that allows you to input a vector containing which columns you wish to look at missing values for (these can be written as strings). Set the default to everything().\n\n\n\n\n\n\n\n\nClick to reveal the solution!\n\n\n\n\n\n\nsummarizeby_fun <- function(data, groups, columns = everything()){\n data %>%\n group_by({{groups}}) %>%\n summarize(across(columns, ~ sum(is.na(.x))))\n}\n\nsummarizeby_fun(penguins, species)\n\nWarning: There was 1 warning in `summarize()`.\nℹ In argument: `across(columns, ~sum(is.na(.x)))`.\nCaused by warning:\n! Using an external vector in selections was deprecated in tidyselect 1.1.0.\nℹ Please use `all_of()` or `any_of()` instead.\n # Was:\n data %>% select(columns)\n\n # Now:\n data %>% select(all_of(columns))\n\nSee <https://tidyselect.r-lib.org/reference/faq-external-vector.html>.\n\n\n# A tibble: 3 × 8\n species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g\n <fct> <int> <int> <int> <int> <int>\n1 Adelie 0 1 1 1 1\n2 Chinstrap 0 0 0 0 0\n3 Gentoo 0 1 1 1 1\n# ℹ 2 more variables: sex <int>, year <int>\n\nsummarizeby_fun(penguins, species, c(\"bill_length_mm\", \"sex\"))\n\n# A tibble: 3 × 3\n species bill_length_mm sex\n <fct> <int> <int>\n1 Adelie 1 6\n2 Chinstrap 0 0\n3 Gentoo 1 5",
0 commit comments