我有一个使用cut
创建的因子变量:
mycuts=cut(c(1,2,3,4,5,6,7,8),breaks = 3)
mycuts
[1] (0.993,3.33] (0.993,3.33] (0.993,3.33] (3.33,5.67] (3.33,5.67]
[6] (5.67,8.01] (5.67,8.01] (5.67,8.01]
Levels: (0.993,3.33] (3.33,5.67] (5.67,8.01]
现在我希望将分布向量otherdata
与cut
的分区相同。
otherdata=c(4,8)
始终cut
的新otherdata
级别与data
的级别不同,我只能设置标签。
所以,我试过了
factor(otherdata,levels=levels(mycuts))
[1] <NA> <NA>
Levels: (0.993,3.33] (3.33,5.67] (5.67,8.01]
但它不起作用。
所需行为( upd on comment ):
[1](3.33,5.67)(5.67,8.01) 等级:(0.993,3.33)(3.33,5.67)(5.67,8.01)
答案 0 :(得分:2)
# breaks vector obtained in a way suggested in ?cut
breaks <- unique(as.numeric(c(sub("\\((.+),.*", "\\1", mycuts),
sub("[^,]*,([^]]*)\\]", "\\1", mycuts))))
cut(c(4, 8), breaks = breaks)
# [1] (3.33,5.67] (5.67,8.01]
# Levels: (0.993,3.33] (3.33,5.67] (5.67,8.01]
答案 1 :(得分:0)
只需将中断保存到某个值并重复使用它们:
data=c(1,2,3,4,5,6,7,8)
mn=min(data)
mx=max(data)
d=(mx-mn)/3
br=seq(from=mn,to=mx,by=d)
mycuts=cut(data,breaks = br, include.lowest=TRUE)
otherdata=c(4,8)
cut(otherdata,breaks = br, include.lowest=TRUE)