繁体   English   中英

在 R 中生成直方图导致错误

[英]generating a histogram in R causing an error

我将数据保存在名为“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'

我认为您的问题是当hist()只知道如何处理data.frame时,您将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 列的默认名称,如果您没有命名它们

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM