Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

r - Cut and labels/breaks length conflict

I am working with the cut function to prep data for a barplot histogram but keep running into a seeming inconsistency between my labels and breaks:

Error in cut.default(sample(1:1e+05, 500, T), breaks = sq, labels = sprintf("$%.0f", : labels/breaks length conflict

Here is an example. I pretend that it is income data, using a sequence of 0 to $100,000 in bins of $10,000. I use the same variable to generate both breaks and labels, with minor formating on the label side. I thought they might for some reason have different lengths when comparing to a character vector, but they appear to have the same length, still.

> sq<-seq(0,100000,10000)
> cut(sample(1:100000, 500, T),breaks=sq,labels=sprintf("$%.0f",sq))
> length(sprintf("$%.0f",sq))
[1] [11]
> length(sq)
[1] [11]

EDIT:

Per @thelatemail and @Josh O'Brien's suggestions, I changed the code to this and things are in working order. cut(sample(1:100000, 500, T),breaks=sq,labels=sprintf("$%.0f",sq[-1]))

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With cut(), 11 breaks delimit 10 levels which will require only 10 labels. The "labels/breaks length conflict" error is telling you that the call to sprintf() is supplying one too many labels to the labels= argument.

In code:

breaks <- 0:10/10
dat <- runif(1e4)

length(breaks)
# [1] 11
length(levels(cut(breaks, sq)))
# [1] 10

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...