I have came across a tricky issue when using the across()
and scale()
functions together.
Here are the sample data:
library(tidyverse)
roster <- tibble(
Student = c("John Davis", "Angela Williams", "Bullwinkle Moose",
"David Jones", "Janice Markhammer", "Cheryl Cushing",
"Reuven Ytzrhak", "Greg Knox", "Joel England",
"Mary Rayburn"),
Math = c(502, 600, 412, 358, 495, 512, 410, 625, 573, 522),
Science = c(95, 99, 80, 82, 75, 85, 80, 95, 89, 86),
English = c(25, 22, 18, 15, 20, 28, 15, 30, 27, 18)
)
I am trying to use mutate()
, across()
, and scale()
to calculate the standardized scores for each subject by using the code below:
roster |>
mutate(across(.cols = c(Math, Science, English), .fns = scale, .names = "z_{.col}"))
While the results are correct, I noticed that the names of the new variables are a bit unusual, as all of them have a suffix [,1]
.
Student Math Science English z_Math[,1] z_Science[,1] z_English[,1]
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 John Davis 502 95 25 0.0127 1.08 0.587
2 Angela Williams 600 99 22 1.14 1.59 0.0367
3 Bullwinkle Moose 412 80 18 -1.03 -0.847 -0.697
The new variables also look different:
I am aware that the function scale()
returns "matrix" "array"
, which may be the cause of the unusuals, but I have no clue how to 'correct' both the names and the type of values.
CodePudding user response:
You can wrap scale
in a vector, i.e.
roster |>
mutate(across(.cols = c(Math, Science, English), .fns = function(i)c(scale(i)), .names = "z_{.col}"))
# A tibble: 10 × 7
Student Math Science English z_Math z_Science z_English
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 John Davis 502 95 25 0.0127 1.08 0.587
2 Angela Williams 600 99 22 1.14 1.59 0.0367
3 Bullwinkle Moose 412 80 18 -1.03 -0.847 -0.697
4 David Jones 358 82 15 -1.65 -0.590 -1.25
5 Janice Markhammer 495 75 20 -0.0681 -1.49 -0.330
6 Cheryl Cushing 512 85 28 0.128 -0.205 1.14
7 Reuven Ytzrhak 410 80 15 -1.05 -0.847 -1.25
8 Greg Knox 625 95 30 1.43 1.08 1.50
9 Joel England 573 89 27 0.832 0.308 0.954
10 Mary Rayburn 522 86 18 0.243 -0.0770 -0.697