简体   繁体   English

在R中提取特定百分比的数据

[英]Extracting specific percentage of data in R

Having a vector of sorted integers, I want to extract 10 percent of data from each extreme. 有一个排序的整数向量,我想从每个极端中提取10%的数据。 For instance having integers from 1 to 100, I want to take 10 percent from left tail of data (1:10) and 10 percent from right tail of data (91:100). 例如,具有从1到100的整数,我想从数据左尾(10:10)取10%,从数据右尾(91:100)右取10%。 I can do it by first sorting data and then dividing into 100 and extracting first and last part. 我可以先对数据进行排序,然后将其划分为100,然后提取第一部分和最后一部分,以完成此操作。 But, I wanted to know if R has a built function for it. 但是,我想知道R是否具有内置功能。

Thanks! 谢谢!

How about a careful use of quantile and sample ?` 仔细使用quantilesample怎么sample

xx <- rnorm(1e3)
thresh <- quantile(xx, c(.1, .9))

left <- xx[xx <= thresh[[1]]]
left <- sample(left, length(left) %/% 10)

right <- xx[xx >= thresh[[2]]]
right <- sample(right, length(right) %/% 10)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM