使用scale()作为.fns时across()中的异常名称

7lrncoxx  于 2023-02-06  发布在  其他
关注(0)|答案(1)|浏览(142)

在一起使用across()scale()函数时,我遇到了一个棘手的问题。
以下是示例数据:

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)
)

我尝试使用mutate()across()scale()计算每个受试者的标准化评分,代码如下:

roster |> 
mutate(across(.cols = c(Math, Science, English), .fns = scale, .names = "z_{.col}"))

虽然结果是正确的,但我注意到新变量的名称有点不寻常,因为它们都有一个后缀[,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

新变量看起来也不同:

我知道函数scale()返回"matrix" "array",这可能是异常的原因,但我不知道如何“纠正”值的名称和类型。

aamkag61

aamkag611#

您可以将scale包裹在向量中,即

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

相关问题