2017-02-13 164 views
1

我保存在名爲「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' 
+0

'numeric'是一類。當您看到「必須是數字」之類的錯誤時,這意味着您的數據屬於錯誤的類別。您應該查看'class(timemaps_data)'或'str(timemaps_data)'來查看它的真實含義 - 這可以幫助您找出問題所在。 – Gregor

+0

你的問題很可能在於你的數據本身。我的賭注是你文件的最後一行。但可能是一個不是數字的字符。如果該假設爲真,請在導入之前清除數據或過濾任何不能強制爲數字的值。 – Mematematica

回答

1

我覺得你的問題是,你與data.frame作爲參數提供hist()時只知道如何應對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的列默認名稱,如果你不爲它們命名

+0

這兩個解決方案都能正常工作。謝謝。但是,直方圖並不是最好看的,因爲數據變化太大。我想讓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) –