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

时间:2017-02-13 22:07:36

标签: r graph histogram

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

1 个答案:

答案 0 :(得分: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)

或:

String -> AttributeValue

&#34; V1&#34;是data.frame的列的默认名称(如果您不为它们命名)