将数据分配给 cut (R) 生成的级别
Assign data to levels produced by cut (R)
我用 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)
otherdata
的新 cut
级别与 data
级别不同,我只能设置标签。
所以,我试过了
factor(otherdata,levels=levels(mycuts))
[1] <NA> <NA>
Levels: (0.993,3.33] (3.33,5.67] (5.67,8.01]
但是不行。
期望的行为(更新评论):
[1] (3.33,5.67] (5.67,8.01]
等级:(0.993,3.33] (3.33,5.67] (5.67,8.01]
只需将中断保存到一个值并重新使用它们:
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)
# 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]
我用 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)
otherdata
的新 cut
级别与 data
级别不同,我只能设置标签。
所以,我试过了
factor(otherdata,levels=levels(mycuts))
[1] <NA> <NA>
Levels: (0.993,3.33] (3.33,5.67] (5.67,8.01]
但是不行。
期望的行为(更新评论):
[1] (3.33,5.67] (5.67,8.01] 等级:(0.993,3.33] (3.33,5.67] (5.67,8.01]
只需将中断保存到一个值并重新使用它们:
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)
# 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]