【问题标题】:generating a histogram in R causing an error在 R 中生成直方图导致错误
【发布时间】:2017-02-13 22:07:36
【问题描述】:

我将数据保存在名为“timemapreport.txt”的文本文件中,我正在尝试将其导入 RStudio 并为其生成直方图:

数据以这种格式保存在文本文件中:

12
16
1025
965
9
1
9
9
12

我尝试使用此代码,但它产生了错误: 正在 RStudio 中读取数据。我认为这很好。但是,当我尝试生成直方图时,由于错误而失败。我在网上做了一些搜索,它说 R 正在读取数据,但将其视为字符串,而不是数字,因此我尝试将其转换为数字或整数,但仍然无效。

我取出函数 hist() 的一些参数,看看是哪一个导致了错误,但这也不起作用。我什至将论点简化为只有一个论点,但仍然没有运气!

感谢任何帮助。

谢谢!

> timemaps_data <- read.table("C:/R/timemapreport.txt", header=F, sep="\t")
> View(timemaps_data)
> View(timemaps_data)
> max_num <- max(timemaps_data)
> hist(timemaps_data, col=heat.colors(max_num), breaks=max_num, xlim=c(0,max_num), right=F, main="Mementos Histogram", las=1)
Error in hist.default(timemaps_data, col = heat.colors(max_num), breaks = max_num,  : 
  'x' must be numeric
> hist(timemaps_data, col=heat.colors(max_num), breaks=max_num, xlim=c(0:max_num), right=F, main="Mementos Histogram", las=1)
Error in hist.default(timemaps_data, col = heat.colors(max_num), breaks = max_num,  : 
  'x' must be numeric
> hist(timemaps_data, breaks=max_num, xlim=c(0,max_num), right=F, main="Mementos Histogram", las=1)
Error in hist.default(timemaps_data, breaks = max_num, xlim = c(0, max_num),  : 
  'x' must be numeric
> hist(timemaps_data, breaks=max_num, right=F, main="Mementos Histogram", las=1)
Error in hist.default(timemaps_data, breaks = max_num, right = F, main = "Mementos Histogram",  : 
  'x' must be numeric
> hist(timemaps_data, right=F, main="Mementos Histogram", las=1)
Error in hist.default(timemaps_data, right = F, main = "Mementos Histogram",  : 
  'x' must be numeric
> hist(timemaps_data, main="Mementos Histogram", las=1)
Error in hist.default(timemaps_data, main = "Mementos Histogram", las = 1) : 
  'x' must be numeric
> hist(timemaps_data, main="Mementos Histogram")
Error in hist.default(timemaps_data, main = "Mementos Histogram") : 
  'x' must be numeric
> hist(timemaps_data)
Error in hist.default(timemaps_data) : 'x' must be numeric
> hist(timemaps_data, col="lightblue", ylim=c(0,10))
Error in hist.default(timemaps_data, col = "lightblue", ylim = c(0, 10)) : 
  'x' must be numeric
> timemaps_data <- read.table("C:/R/timemapreport.txt", header=F, sep="\n")
> max_num <- max(timemaps_data)
> hist(timemaps_data, col=heat.colors(max_num), breaks=max_num, xlim=c(0,max_num), right=F, main="Mementos Histogram", las=1)
Error in hist.default(timemaps_data, col = heat.colors(max_num), breaks = max_num,  : 
  'x' must be numeric
> timemaps_data <- as.numeric(timemaps_data) 
Error: (list) object cannot be coerced to type 'double'
> timemaps_data <- as.int(timemaps_data) 
Error: could not find function "as.int"
> timemaps_data <- as.integer(timemaps_data) 
Error: (list) object cannot be coerced to type 'integer'
> timemaps_data <- as.integer(timemaps_data) 
Error: (list) object cannot be coerced to type 'integer'

【问题讨论】:

  • numeric 是一个类。当您看到“必须是数字”之类的错误时,这意味着您的数据属于错误的类别。您应该查看 class(timemaps_data)str(timemaps_data) 以了解其真正含义 - 这可以帮助您找出问题所在。
  • 您的问题很可能在于您的数据本身。我敢打赌是你文件的最后一行。但可能是某个不是数字的字符。如果该假设成立,请在导入之前清理数据或过滤任何无法强制转换为数字的值。

标签: r graph histogram


【解决方案1】:

我认为您的问题是,当 hist() 只知道如何处理 data.frame 的单个列时,您提供 data.frame 作为其参数。试试这个:

timemaps_data <- read.table("C:/R/timemapreport.txt", header=F, sep="\t")
hist(timemaps_data[,1])

或:

timemaps_data <- read.table("C:/R/timemapreport.txt", header=F, sep="\t")
names(timemaps_data)
hist(timemaps_data$V1)

“V1”是 data.frame 列的默认名称(如果您不命名它们)

【讨论】:

  • 两种解决方案都有效。谢谢。然而,直方图并不是最好看的,因为数据变化太大。我想让 bin 大小为 1,因此 x 轴上的数据不会组合在一起,并使每个值看起来像一个条形。如果 x 轴上的某个值没有频率,则将其移除以为有频率的值腾出空间。我试过这段代码,但不是我想要的。我很高兴能够生成直方图。谢谢.. hist(timemaps_data$V1, col=heat.colors(max_num), breaks=max_num, xlim=c(0,max_num), right=F, main="Mementos Histogram", las=1)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-22
  • 1970-01-01
  • 1970-01-01
  • 2018-12-06
  • 1970-01-01
  • 2019-09-02
  • 2017-07-20
相关资源
最近更新 更多