Search code examples
rr-factor

Assign data to levels produced by cut (R)


I have a factor variable created with 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]

Now I want distribute vector otherdata to the same intervals as cut did.

otherdata=c(4,8)

A new cut always for otherdata has levels different from that data has, and I can set only labels.

So, I've tried

factor(otherdata,levels=levels(mycuts))

[1] <NA> <NA>
Levels: (0.993,3.33] (3.33,5.67] (5.67,8.01]

But it does not work.

The desired behaviour (upd on comment):

[1] (3.33,5.67] (5.67,8.01] Levels: (0.993,3.33] (3.33,5.67] (5.67,8.01]


Solution

  • # 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]