r - 如何在 cut 函数中获取分位数标签名称?

标签 r

我有这段代码,

breaks=quantile(foo1$mnqtp, probs=seq(0,1, by=0.2))

尝试 1

foo1$quantile <- with(foo1, cut(mnqtp, breaks=quantile(mnqtp, probs=seq(0,1, by=0.2)),
                            labels=c("0%","20%","40%","60%","80%"),
                            include.lowest=TRUE))

尝试 2

foo1$quantile <- with(foo1, cut(mnqtp, breaks),labels=names(breaks),include.lowest=TRUE)

我只想根据分位数将标签自动重命名为 90%、100%、20% 等。我可以使用 Try 1 手动完成,但我想对此有一个自动解决方案。你能帮忙吗?

最佳答案

也许是这样的?

foo1$quantile <- with(foo1, cut(mnqtp, breaks=qu <- quantile(mnqtp, probs=seq(0,1, by=0.2)),
                      labels=names(qu)[-1],
                      include.lowest=TRUE))

可重现的例子

 iris$quantile <- with(iris, cut(Sepal.Width,
   breaks = qu <- quantile(Sepal.Width, probs = seq(0,1, by=0.2)),
   labels = names(qu)[-1], include.lowest=TRUE))
 head(iris$quantile)
 # [1] 100% 40%  80%  60%  100% 100%
 # Levels: 20% 40% 60% 80% 100%

关于r - 如何在 cut 函数中获取分位数标签名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25086437/

相关文章:

r - 10 行 block 中 p 值的循环改组

r - 理解 R 中的词法作用域

r - 如何刷新R中的打印缓冲区?

r - 向使用ggplot创建的密度图添加摘要信息

regex - 如何从 R 公式字符串中删除系数

r - 使用 data.table 根据 R 中的 B 列有条件地删除 A 列中匹配的行

R options(digits=2) 函数改变总位数的格式。寻找一种更改小数点后数字的方法

r - 使用 quosure 作为 by 参数连接数据集

r - 如果在 R 中模拟后使用鼠标滚轮,Windows 7 将变得无响应

r - 序列和 seqplot 之间不一致