标准化数据框中的一个变量

时间:2019-12-10 13:10:37

标签: r

我想标准化我的数据,而不是完整的数据框,而只是一个变量。但是,当我使用scale函数时,得到的结果似乎是错误的,因为当我测试sd并表示它们不等于1和0时。此外,我尝试使用此代码来实现。但是,此处的均值和sd值也不正确。我也尝试过标准化包。

  1. Cox<-(Cox$active - mean(Cox$active)) / sd(Cox$active)

  2. Cox$Use<-standardize(Cox$active)

输出看起来像这样:

std.dev  5.084175e+01     
mean  4.816038e+01

Cox $ active的投放量

23.129032258065, 12.409090909091, 70.025157232704, 11.125, 12.409090909091, 
89.006289308176, 89.410526315789, 48.66, 35.747252747253, 94.076086956522, 
90.8, 53.284403669725, 72.747572815534, 72.747572815534, 94.076086956522, 
94.076086956522, 74.264516129032, 73.306930693069, 94.076086956522, 
3.6153846153846, 94.076086956522, 68.933333333333, 94.076086956522, 
59.528169014085, 14.091743119266, 94.076086956522, 55.5, 94.076086956522, 
23.882978723404, 90.854166666667, 44.045454545455, 55.5, 53.769230769231, 
90.854166666667, 29.467391304348, 94.076086956522, 74.247422680412, 
11.470588235294, 47.85393258427, 4.3620689655172, 55.5, 38.747252747253, 
12.409090909091, 94.076086956522, 53.284403669725, 11.470588235294, 
23.314606741573, 63.206896551724, 88.85625, 11.30612244898, 42.696078431373, 
55.5, 29.467391304348, 104.96026490066, 11.470588235294, 55.5, 
62.466019417476, 109.63636363636, 60.666666666667, 42.47619047619, 
68.933333333333, 88.787878787879, 62.466019417476, 10.161290322581, 
88.9, 55.5, 52.777777777778, 63.466666666667, 5.1525423728814, 
47.85393258427, 93.298701298701, 5.1525423728814, 59.528169014085, 
29.467391304348, 12.409090909091, 94.076086956522, 94.076086956522,

``

2 个答案:

答案 0 :(得分:2)

我无法复制您的问题:

x <- c(23.129032258065,12.409090909091,70.025157232704,11.125,12.409090909091,89.006289308176,89.410526315789,48.66,35.747252747253,94.076086956522,90.8,53.284403669725,72.747572815534,72.747572815534,94.076086956522,94.076086956522,74.264516129032,73.306930693069,94.076086956522,3.6153846153846,94.076086956522,68.933333333333,94.076086956522,59.528169014085,14.091743119266,94.076086956522,55.5,94.076086956522,23.882978723404,90.854166666667,44.045454545455,55.5,53.769230769231,90.854166666667,29.467391304348,94.076086956522,74.247422680412,11.470588235294,47.85393258427,4.3620689655172,55.5,38.747252747253,12.409090909091,94.076086956522,53.284403669725,11.470588235294,23.314606741573,63.206896551724,88.85625,11.30612244898,42.696078431373,55.5,29.467391304348,104.96026490066,11.470588235294,55.5,62.466019417476,109.63636363636,60.666666666667,42.47619047619,68.933333333333,88.787878787879,62.466019417476,10.161290322581,88.9,55.5,52.777777777778,63.466666666667,5.1525423728814,47.85393258427,93.298701298701,5.1525423728814,59.528169014085,29.467391304348,12.409090909091,94.076086956522,94.076086956522)

x.standard <- (x - mean(x)) / sd(x)

> mean(x.standard)
[1] -7.059987e-18

> sd(x.standard)
[1] 1

答案 1 :(得分:2)

我认为函数scale在您的规范化过程中没有出错,即

nv <- scale(v)

然后

> mean(nv)
[1] -7.059987e-18
> sd(nv)
[1] 1

数据

v <- c(23.129032258065, 12.409090909091, 70.025157232704, 11.125, 
12.409090909091, 89.006289308176, 89.410526315789, 48.66, 35.747252747253, 
94.076086956522, 90.8, 53.284403669725, 72.747572815534, 72.747572815534, 
94.076086956522, 94.076086956522, 74.264516129032, 73.306930693069, 
94.076086956522, 3.6153846153846, 94.076086956522, 68.933333333333, 
94.076086956522, 59.528169014085, 14.091743119266, 94.076086956522, 
55.5, 94.076086956522, 23.882978723404, 90.854166666667, 44.045454545455, 
55.5, 53.769230769231, 90.854166666667, 29.467391304348, 94.076086956522, 
74.247422680412, 11.470588235294, 47.85393258427, 4.3620689655172, 
55.5, 38.747252747253, 12.409090909091, 94.076086956522, 53.284403669725, 
11.470588235294, 23.314606741573, 63.206896551724, 88.85625, 
11.30612244898, 42.696078431373, 55.5, 29.467391304348, 104.96026490066, 
11.470588235294, 55.5, 62.466019417476, 109.63636363636, 60.666666666667, 
42.47619047619, 68.933333333333, 88.787878787879, 62.466019417476, 
10.161290322581, 88.9, 55.5, 52.777777777778, 63.466666666667, 
5.1525423728814, 47.85393258427, 93.298701298701, 5.1525423728814, 
59.528169014085, 29.467391304348, 12.409090909091, 94.076086956522, 
94.076086956522)