我有100个排序值的向量
x=rnorm(100)
x_sort=sort(x)
如何从x_sort
的上部下方删除2.5%(例如)?
答案 0 :(得分:2)
使用quantile
是一种方式:
x=rnorm(100)
x_sort=sort(x)
x_sort2 <- x_sort[ x_sort > quantile(x_sort,0.025) & x_sort < quantile(x_sort,0.975)]
#x_sort2 will be a subset of x_sort starting from the 2.5th quantile and finishing at the 97.5th on this occassion.
#in bigger datasets it's extremely accurate (now you only have 100 values and you cannot pick the 2.5th for example).
> length(x_sort)
[1] 100
> length(x_sort2)
[1] 94
您可以将0.025或0.975替换为您想要的下限或上限值,甚至可以添加一个功能来为您完成。
这样的事情可能是:
excluder <- function(myvec,a,b) { myvec[myvec > quantile(myvec,a) & myvec < quantile(myvec,b)] }
#with myvec being your vector, a the lower limit and b the upper limit
x_sort3 <- excluder(x_sort,0.025,0.975)
> length(x_sort3)
[1] 94
答案 1 :(得分:0)
这是一个选项:
m <- 5 # percentage to be deleted from each side
l <- length(x_sort)
n <- l * m/100
y <- head(tail(x_sort, l - n), l - n*2)
length(x_sort)
#[1] 100
length(y)
#[1] 90
您可以将n
的值四舍五入为整数或使用floor
或ceiling
个函数,例如n <- round(l * m/100)
以确保您不会尝试删除例如x的2.3元素。
另一种方法:
m <- 5 # percentage to be deleted from each side
l <- length(x_sort)
n <- round(l * m/100)
y2 <- x_sort[seq.int(n+1L, l-n, 1L)]
他们会退回吗?
all.equal(y, y2)
#[1] TRUE