将数据分配到cut(R)

时间:2016-02-20 20:52:52

标签: r r-factor

我有一个使用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]

现在我希望将分布向量otherdatacut的分区相同。

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)

2 个答案:

答案 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)