我有这样一个向量:
a<-c(0.8,1,1.2,3,4,5,6)
每个值都用年份表示,我想将它们分为三个类别(“一年以下”,“一年至五年以上”和“五年以上”)
我的代码如下:
AAA_factor <- cut(x = a, breaks = c(0, 1, 5),labels = c("One year or less","Over one year to five years", "Over five years"))
当我运行这段代码时,它会向我返回此错误:
Error in cut.default(x = a, breaks = c(0, 1, 5), labels = c("One year or less", :
lengths of 'breaks' and 'labels' differ
如何解决此问题?
我还想将子分组添加为a的另一列。
尝试过:
a_group <-data.frame(a,cut(x = a, breaks = c(0, 1, 5),labels = c("One year or less","Over one year to five years", "Over five years")))
names(a_group)[2]<-"Time bucket"
有更好的方法吗?
答案 0 :(得分:1)
三个端点将仅给出2个区域。而是添加无限大的上限
AAA_factor <- cut(x = a, breaks = c(0, 1, 5, Inf),
labels = c("One year or less","Over one year to five years",
"Over five years"))
AAA_factor
[1] One year or less One year or less
[3] Over one year to five years Over one year to five years
[5] Over one year to five years Over one year to five years
[7] Over five years