我有这段代码,
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/