R直方图范围错误:某些"x"未计数;也许“中断"的范围不超过"x"的范围 [英] R histogram range error: some 'x' not counted; maybe 'breaks' do not span range of 'x

查看:667
本文介绍了R直方图范围错误:某些"x"未计数;也许“中断"的范围不超过"x"的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,我想用R中的hist进行绘制.数据集中有许多行的值超出了我关心的值.具体来说,我的R脚本是:

I have a dataset that I'd like to plot with hist in R. There are a number of rows in the dataset whose values are beyond a value that I care about. Specifically, my R script is:

library(ggplot2)    
data = read.table("input.txt", sep=" ", strip.white=TRUE, header=TRUE)
pdf("out.pdf")
hist(data$actions,breaks=seq(0,130,by=1))
dev.off()

input.txt的示例数据集是:

name actions
foo 3
bar 129
baz 131

如果我运行R脚本,则会收到错误消息:

If I run the R script, I get an error:

hist.default中的错误(data $ actions,breaks = seq(0,130,by = 1),:
一些"x"不计算在内;也许中断"的范围不超过"x"的范围
调用:hist-> hist.default
执行停止

Error in hist.default(data$actions, breaks = seq(0, 130, by = 1), :
some 'x' not counted; maybe 'breaks' do not span range of 'x'
Calls: hist -> hist.default
Execution halted

我知道为什么会发生此错误:出现了一个大于130的值,即baz值为131.

I know why this error occurs: there is one occurrence of a value greater than 130, namely baz with a value of 131.

我想要创建一个直方图 just ,用于指定范围为0到130的频率,并且所有超出该范围的频率都将被静默忽略.我该怎么办?

What I'd like is to create a histogram just for the frequencies in the specified range of 0 to 130, and for all frequencies out of that range to be silently ignored. How can I do this?

推荐答案

避免此错误的最佳方法是对您提供给基本R函数hist的数据进行子集化.

The best way to avoid this error is to subset the data that you feed to the base R function hist.

例如

with(data, hist(actions[actions >= 0 & actions < 131], breaks=seq(0,130,by=1))

也许更灵活的方法是预先指定所需的一组值,以便在某些时候改变主意时更容易进行调整.

Maybe a little more flexible approach is to pre-specify the desired set of values, to make it easier to adjust if you change your mind at some point.

myValues <- seq_len(131)-1
with(data, hist(actions[actions %in% myValues], breaks=myValues)

这篇关于R直方图范围错误:某些"x"未计数;也许“中断"的范围不超过"x"的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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