计算新的数据框变量与列平均值的距离
我有一个看起来像这样的数据框。
用户 | V1 | V2 | V3 |
---|---|---|---|
Jim | .34 | .33 | .88 |
David | .54 | .34 | .71 |
Scott | .12 | .25 | .12 |
Frank | .76 | .76 | .44 |
Doug | .68 | .09 | .54 |
Tom | .91 | .67 | .92 |
但我想要来计算一个新的变量。我希望通过从相应变量 (V1< /code>、
V2
、V3
)来自列的平均值。例如,V1
列的平均值为 0.55。所以对于 Jim,我希望方程为 0.34 - 0.55 = -0.21。对于V1_DISTfromMean
。生成的数据框将类似于下面的数据框,其中填充了所有值。
User | V1 | V2 | V3 | V1_DISTfromMEAN | V2_DISTfromMEAN | V1_DISTfromMEAN |
---|---|---|---|---|---|---|
Jim | .34 | .33 | .88 | - .21 | ??? | ??? |
大卫 | .54 | .34 | .71 | - .01 | ??? | ??? |
斯科特 | .12 | .25 | .12 | ??? | ??? | ??? |
弗兰克 | .76 | .76 | .44 | ??? | ??? | ??? |
道格 | .68 | .09 | .54 | ??? | ??? | ??? |
汤姆 | .91 | .67 | .92 | ??? | ??? | ??? |
任何帮助将不胜感激。如果我未能包含所有必要的数据,请告诉我。
I have a dataframe that look something like this.
User | V1 | V2 | V3 |
---|---|---|---|
Jim | .34 | .33 | .88 |
David | .54 | .34 | .71 |
Scott | .12 | .25 | .12 |
Frank | .76 | .76 | .44 |
Doug | .68 | .09 | .54 |
Tom | .91 | .67 | .92 |
But I would like to calculate a new variables. I want the new variables (V1_DISTfromMEAN
, V2_DISTfromMEAN
, V3_DISTfromMEAN
) to be calculated by subtracting each observation from their corresponding variables (V1
, V2
, V3
) from the column's mean value. For example, the mean for the column V1
is .55. So for Jim, I would want the equation to be .34 - .55 = -0.21. for V1_DISTfromMean
. The resulting dataframe would look something like the one below, with all values filled in.
User | V1 | V2 | V3 | V1_DISTfromMEAN | V2_DISTfromMEAN | V1_DISTfromMEAN |
---|---|---|---|---|---|---|
Jim | .34 | .33 | .88 | - .21 | ??? | ??? |
David | .54 | .34 | .71 | - .01 | ??? | ??? |
Scott | .12 | .25 | .12 | ??? | ??? | ??? |
Frank | .76 | .76 | .44 | ??? | ??? | ??? |
Doug | .68 | .09 | .54 | ??? | ??? | ??? |
Tom | .91 | .67 | .92 | ??? | ??? | ??? |
Any help would be greatly appreciated.Let me know if I've failed to include all the necessary data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 colMeans 获取均值向量,从输入数据集中减去,注意 R 的表操作按列主顺序排列,将原始数据与结果绑定。
由 reprex 软件包 (v2.0.1) 创建于 2022 年 3 月 2 日
Use
colMeans
to get a vector of means, subtract from the input data set taking care that R's table operations are in column major order, bind the original with the result.Created on 2022-03-02 by the reprex package (v2.0.1)
我们可以使用
cross
:We could use
across
:一个可能的解决方案,基于
dplyr
:A possible solution, based on
dplyr
: