R Dplyr变异,计算每一行的标准差 [英] R Dplyr mutate, calculating standard deviation for each row
本文介绍了R Dplyr变异,计算每一行的标准差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试计算数据框中某些列的均值和标准差,并将这些值返回到数据框中的新列。我可以用它来表示平均值:
I am trying to calculate the mean and standard deviation from certain columns in a data frame, and return those values to new columns in the data frame. I can get this to work for mean:
library(dplyr)
mtcars = mutate(mtcars, mean=(hp+drat+wt)/3)
但是,当我尝试对标准偏差执行相同操作时,我有一个问题,因为我无法像我那样轻易地对等式进行硬编码。因此,我尝试使用如下函数:
However, when I try to do the same for standard deviation, I have an issue, because I cannot hardcode the equation like I did for mean very easily. So, I try to use a function, as follows:
mtcars = mutate(mtcars, mean=(hp+drat+wt)/3, stdev = sd(hp,drat,wt))
导致错误错误在sd(hp,drat,wt)中:未使用的参数(wt)。如何纠正我的语法?谢谢。
Resulting in the error "Error in sd(hp, drat, wt) : unused argument (wt)". How can I correct my syntax? Thank you.
推荐答案
您可以尝试
library(dplyr)
library(matrixStats)
nm1 <- c('hp', 'drat', 'wt')
res1 <- mtcars %>%
mutate(Mean= rowMeans(.[nm1]), stdev=rowSds(as.matrix(.[nm1])))
head(res1,3)
# mpg cyl disp hp drat wt qsec vs am gear carb Mean stdev
#1 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 38.84000 61.62969
#2 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 38.92500 61.55489
#3 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 33.05667 51.91809
或使用 do
res2 <- mtcars %>%
rowwise() %>%
do(data.frame(., Mean=mean(unlist(.[nm1])),
stdev=sd(unlist(.[nm1]))))
head(res2,3)
# mpg cyl disp hp drat wt qsec vs am gear carb Mean stdev
#1 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 38.84000 61.62969
#2 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 38.92500 61.55489
#3 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 33.05667 51.91809
这篇关于R Dplyr变异,计算每一行的标准差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文