Function that converts a vector of numbers to a vector of standard units

前端 未结 3 1360
长情又很酷
长情又很酷 2020-12-30 07:33

Is there a function that given a vector of numbers, returns another vector with the standard units corresponding to each value?

where standard unit: how many SDs a v

3条回答
  •  情话喂你
    2020-12-30 08:30

    The function you are looking for is scale.

    scale(x)
    
    
               [,1]
    [1,] -1.3416408
    [2,] -0.4472136
    [3,]  0.0000000
    [4,]  0.4472136
    [5,]  1.3416408
    attr(,"scaled:center")
    [1] 4
    attr(,"scaled:scale")
    [1] 2.236068
    

    Note that the answers are not identical to what you posted in your question. The reason is that the standard deviation in your x is actually 2.23, not 2.

    sd(x)
    [1] 2.236068
    

提交回复
热议问题