作者s3714443 (metalheads)
看板R_Language
标题Re: [问题] 由大排到小分五群
时间Tue Apr 10 14:40:25 2018
自问自答
这个问题是高百分位累积太多重复值
我在stackoverflow发现有个高手自己写函数来解决
先做一个符合特性的向量 并想分5群
x=c(rep(10,20),runif(50,2,10))
findInterval(x,quantile(x,probs = 0:5/5),
rightmost.closed=T,left.open = T )
会发现只能分4群
hmisc的cut2也是
cut2(x,g=5) %>% as.numeric()
自制函数可以成功分出5群
ntile=function(x,n){
b <- x[!is.na(x)]
q <- floor((n * (rank(b, ties.method = "first")-1)/length(b))+1)
d <- rep(NA, length(x))
d[!is.na(x)] <- q
return(d)
}
大家可以试试看
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 140.117.72.53
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/R_Language/M.1523342429.A.21C.html
1F:推 x88776544pc: 如果要保留原始资料分布的特性,用cut( x, b=5 )较好 04/10 15:39