You can complete these exercises in your local version of the .qmd file. Either download a copy of the whole book from github (see introduction), or download this .qmd using the download button on the top right of the page.
Write functions for each of the following. Remember that you can write pseudocode first if it helps.
22.1 Converting and tidying
22.1.1 Convert milliseconds to seconds
22.1.2 Convert n_months to n_years and round all values up the next year
Use ceiling() to round all values ending in something other than .0 up to the next whole value.
22.1.3 Remove the ’ ms’ suffix from data like “2000 ms”
Use the {stringr} library.
22.1.4 Tidy messy open-ended gender data
You can use the dat_depression_rct tibble that was created above.
22.1.5 Tidy messy age data
You can use the dat_depression_rct tibble that was created above.
22.2 Summarizing
22.2.1 Calculate mean by group
You can use the dat_depression_rct tibble that was created above.
What additional arguments might this benefit from to avoid it breaking under common use cases?
22.2.2 Calculate mean, SD, and N by group
You can use the dat_depression_rct tibble that was created above.
# Exercises for 'Writing functions' chapter <span class="badge badge-draft3">✎ Polishing</span>```{r}#| include: false# if it is available, run the setup script that tells quarto to round all df/tibble outputs to three decimal placesif(file.exists("../_setup.R")){source("../_setup.R")}```These exercises accompany the [Writing Functions chapter](../chapters/3_writing_functions.qmd).You can complete these exercises in your local version of the .qmd file. Either download a copy of the whole book from github (see introduction), or download this .qmd using the download button on the top right of the page.Write functions for each of the following. Remember that you can write pseudocode first if it helps.```{r}#| include: false# create dataset to be used/cleaned# install the devtools and truffle packages if you don't have them already:# library(devtools)# install_github("ianhussey/truffle")library(truffle)library(tibble)dat_depression_rct <-truffle_likert(study_design ="factorial_between2",n_per_condition =20,factors ="X1_latent",prefixes ="X1_item",alpha = .70,n_items =13,n_levels =4,approx_d_between_groups =0.40) |>snuffle_sum_scores("X1_", min =1, max =4, id_col ="id") |>select(id, group = condition, score = X1_sumscore) |>mutate(score = score -13) |>dirt_demographics()```## Converting and tidying### Convert milliseconds to seconds```{r}```### Convert n_months to n_years and round all values up the next year Use `ceiling()` to round all values ending in something other than .0 up to the next whole value.```{r}```### Remove the ' ms' suffix from data like "2000 ms" Use the {stringr} library.```{r}```### Tidy messy open-ended gender dataYou can use the `dat_depression_rct` tibble that was created above.```{r}```### Tidy messy age dataYou can use the `dat_depression_rct` tibble that was created above.```{r}```## Summarizing### Calculate mean by groupYou can use the `dat_depression_rct` tibble that was created above.What additional arguments might this benefit from to avoid it breaking under common use cases?```{r}```### Calculate mean, SD, and N by groupYou can use the `dat_depression_rct` tibble that was created above.```{r}#data <- dat_depression_rctcalc_summary <-function(data, grouping, variable){ output <- data %>%group_by({{ grouping }}) %>%summarize(mean =mean({{ variable }}),sd =sd({{ variable }}),n =n())return(output)}calc_summary(dat_depression_rct, group, score)```### Mean, SD, and N by group, and differences in means between groupsYou can use the `dat_depression_rct` tibble that was created above.Pivot the result to wider format before mutating the difference in means.```{r}```