John_West
John_West

Reputation: 2399

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]

Upvotes: 2

Views: 1432

Answers (2)

Julius Vainora
Julius Vainora

Reputation: 48211

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

Upvotes: 2

John_West
John_West

Reputation: 2399

Just save breaks to a value and reuse them:

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)

Upvotes: 0

Related Questions