问题描述
我想在删除列中的最小值和最大值之后,计算出所选列中数据帧中每一行的标准差。下面是一个示例:
I would like to compute the standard deviation for each row in a data frame over a selection of columns after removing the minimum and the maximum in that selection. Here is an example:
set.seed(1)
dat <- data.frame(matrix(sample(c(1:100), 10, replace=TRUE), ncol=5))
I设法为每行计算我感兴趣的列的标准差(1:4):
I managed to calculate the sd of my columns of interest (1:4) for each row:
dat <- transform(dat, sd = apply(dat[,1:4], 1, sd))
show(dat)
X1 X2 X3 X4 X5 sd
1 27 58 21 95 63 33.95463
2 38 91 90 67 7 24.93324
但是,我不知道如何排除 min(dat [1,1:4])
和 max(dat [1,1:4])
,然后计算 sd()
。
结果应该是这样的:
However, I can't figure out how to exclude min(dat[1,1:4])
and max(dat[1,1:4])
before calculating sd()
.The result should be this:
X1 X2 X3 X4 X5 sd
1 27 58 21 95 63 21.92031 # notice: sd calculated by hand using 'sd(c(27,58))'
2 38 91 90 67 7 16.26346 # notice: sd calculated by hand using 'sd(c(67,90))'
有人可以帮我吗?
推荐答案
您可以编写一个自定义函数来为您执行此操作。它接受一个向量,删除最小值和最大值,然后返回剩余值的sd。当然,您也可以将其编写为匿名函数,但有时将函数分开会使代码更具可读性。
You could write a custom function to do this for you. It takes in a vector, removes the minimum and maximum, and returns the sd of the remaining values. Of course you could also write this as an anonymous function, but sometimes having the function separate makes the code more readable.
sd_custom <- function(x){
x <- x[x!=min(x) & x!=max(x)]
return(sd(x))
}
dat$sd <- apply(dat[,1:4], 1, sd_custom)
> dat
X1 X2 X3 X4 X5 sd
1 27 58 21 95 63 21.92031
2 38 91 90 67 7 16.26346
这篇关于R:在删除最小值和最大值之后,计算选定列中每一行的SD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!