切割和标签/断裂长度冲突 [英] Cut and labels/breaks length conflict

查看:70
本文介绍了切割和标签/断裂长度冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用cut函数来准备条形图直方图的数据,但是在标签和中断之间似乎一直看起来不一致:

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:

cut.default(sample(1:1e + 05,500,T)中的错误,breaks = sq,标签= sprintf( $%。0f,:
标签/中断长度冲突

这里是一个例子,我假装它是收入数据,使用0到$ 100,000的序列,每个10,000美元,我使用相同的变量来生成这两个数据我认为它们与字符向量相比可能具有不同的长度,但它们似乎仍然具有相同的长度。

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]

编辑:

每个@thelatemail和@ Josh O’Brien的建议,我将代码更改为此,一切正常。
cut(sample(1:100000,500,T),breaks = sq,labels = sprintf( $%。0f,sq [-1]))

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

推荐答案

使用 cut(),有11个中断定义了10个级别仅需要10个标签。 标签/分隔符长度冲突 错误告诉您对 sprintf()的调用提供了太多 labels = 参数的标签。

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.

在代码中:

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

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

这篇关于切割和标签/断裂长度冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆