3 回答
TA贡献1827条经验 获得超9个赞
查看?cut并指定breaks(也许labels)。
x$bins <- cut(x$rank, breaks=c(0,4,10,15), labels=c("1-4","5-10","10-15"))
x
# rank name info bins
# 1 1 steve red 1-4
# 2 3 joe blue 1-4
# 3 6 john green 5-10
# 4 3 liz yellow 1-4
# 5 15 jon pink 10-15
TA贡献1998条经验 获得超6个赞
dat <- "rank,name,info
1,steve,red
3,joe,blue
6,john,green
3,liz,yellow
15,jon,pink"
x <- read.table(textConnection(dat), header=TRUE, sep=",", stringsAsFactors=FALSE)
x$bins <- cut(x$rank, breaks=seq(0, 20, 5), labels=c("1-5", "6-10", "11-15", "16-20"))
x
rank name info bins
1 1 steve red 1-5
2 3 joe blue 1-5
3 6 john green 6-10
4 3 liz yellow 1-5
5 15 jon pink 11-15
TA贡献1874条经验 获得超12个赞
我们可以smart_cut
从包中使用cutr
:
# devtools::install_github("moodymudskipper/cutr")library(cutr)
使用@Andrie的样本数据:
x$bins <- smart_cut(x$rank, c(1,5,11,16), labels = ~paste0(.y[1],'-',.y[2]-1), simplify = FALSE)# rank name info bins# 1 1 steve red 1-4# 2 3 joe blue 1-4# 3 6 john green 5-10# 4 3 liz yellow 1-4# 5 15 jon pink 11-15
- 3 回答
- 0 关注
- 497 浏览
添加回答
举报