profandyfield / discovr

discovr package for R to accompany Discovering Statistics Using R and RStudio
133 stars 25 forks source link

Error using across() function within a mutate - Tutorial 10 #3

Closed jamie-moffatt closed 3 years ago

jamie-moffatt commented 3 years ago

There appears to be an error in Tutorial 10 when using the across() function.

The tutorial asks you to create centred versions of a couple of variables in one go, and the code provided to do that is the following:

vids_tib <- vids_tib %>%
  dplyr::mutate(
    dplyr::across(c(vid_game, caunts), cent = centre)
    )

This is meant to create two variables called "vid_game_cent" and "caunts_cent", which are the centred versions of the original variables. For me it doesn't create those variables and doesn't seem to do anything else.

I had a look athe across() function and it appears the correct way to specify the names of the new variables is with the .names argument:

vids_tib <- vids_tib %>%
  dplyr::mutate(
    dplyr::across(c(vid_game, caunts), centre, .names = "{.col}_cent")
    ) 
profandyfield commented 3 years ago

You can do it like that, but that wasn't what I indended. What's missing is 'list()':

vids_tib <- vids_tib %>% 
  dplyr::mutate(
    dplyr::across(c(vid_game, caunts), list(cent = centre))
    )

I also noticed some general lack of clarity in that section so I've re-written a bit and updated on here. If you reinstall you'll see the changes to that section (if it matters to you!). Thanks for pointing this out.