1

I would like to ask for your advice on how to add mean_center = True to the following code:

library(dplyr)

dataset <- dataset %>%
    mutate_at(.vars = colnames(dataset[5:10]),
              .funs = list("c" = center_scale))

mean_center is an argument of center_scale

For example, a sample data is:

structure(list(ï..x1 = c(2.6, 3.8, 2.6, 4.3, 2.6, 4.4, 4.4, 2.6, 
    2.6, 2.6), x2 = c(3.2, 3.2, 3.2, 4, 3.2, 2.2, 2.2, 3.2, 3.2, 
    3.2), x3 = c(4.5, 3.5, 4.5, 3, 4.5, 4, 4, 4.5, 4.5, 4.5), x4 = c(2.4, 
    3, 2.4, 3.1, 2.4, 4.3, 4.3, 2.4, 2.4, 2.4), x5 = c(3.8, 2.6, 
    4.3, 2.6, 4.4, 2.6, 4.3, 3.4, 3.4, 3.4), x6 = c(3.2, 3.2, 4, 
    3.2, 2.2, 3.2, 2.6, 2.8, 3.9, 1.3), x7 = c(3.5, 4.5, 3, 4.5, 
    4, 4.5, 2.9, 3.7, 3.5, 4.8), x8 = c(3, 2.4, 3.1, 2.4, 4.3, 2.4, 
    4.1, 3, 3.4, 2.7), x9 = c(3.9, 4, 4, 4, 3.9, 4, 3.5, 4.1, 4.3, 
    3.7), x10 = c(4, 3.9, 4, 4, 4, 3.9, 3.9, 4, 4, 4)), class = "data.frame", row.names = c(NA, 
    -10L))
Jose
  • 421
  • 3
  • 10
user14250906
  • 197
  • 8
  • 1
    It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. – stefan Feb 28 '22 at 18:19
  • 1
    `mutate_at(.vars = colnames(dataset[5:10]), .funs = list("c" = center_scale), mean_center = TRUE)` should do it. `?mutate_at` says additional arguments are passed to the `.funs` functions. – Gregor Thomas Feb 28 '22 at 18:22
  • 2
    Using the newer syntax you can use `dataset %>% mutate_at(across(5:10, ~center_scale(.x, mean_center=TRUE))` – MrFlick Feb 28 '22 at 18:22
  • 2
    @MrFlick, isn't the newer format without `_at`, instead `mutate(across(5:10, ...))`? – r2evans Feb 28 '22 at 20:15
  • 1
    @r2evans yes. Indeed. Copy/paste typo. Thanks for catching that. – MrFlick Feb 28 '22 at 20:19
  • @MrFlick This is nice, but it still says 'unused argument'. – user14250906 Mar 01 '22 at 03:35

0 Answers0