我保存在名爲「timemapreport.txt」的文本文件,我想導入到RStudio併爲它生成一個直方圖數據:生成中的R直方圖導致錯誤
的數據被保存在一個這種格式的文本文件:
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)'來查看它的真實含義 - 這可以幫助您找出問題所在。 – Gregor
你的問題很可能在於你的數據本身。我的賭注是你文件的最後一行。但可能是一個不是數字的字符。如果該假設爲真,請在導入之前清除數據或過濾任何不能強制爲數字的值。 – Mematematica