2013-04-20 53 views
0

該R代碼:向量未被設置正確

avector <- as.vector(top.links.added.overall$Amount) 
x <- as.vector(top.links.added.overall[order(avector),]) 
x$Amount <- factor(x$Amount) 
x$color[x$Amount == 100] <- "red" 
x$color[x$Amount == 500] <- "blue" 
x$color[x$Amount == 1000] <- "darkgreen" 
dotchart(x$Amount, 
     labels = row.names(x), 
     cex=.7, 
     groups = x$Amount, 
     gcolor = "black", 
     color = x$color, 
     pch=19, 
     main = "Gas Mileage for Car Models\ngrouped by cylinder", 
     xlab = "Miles Per Gallon") 

返回此錯誤:

Error in dotchart(x$Amount, labels = row.names(x), cex = 0.7, groups = x$Amount, : 
    'x' must be a numeric vector or matrix 

這是top.links.added.overall數據文件:

Amount,Name 
1000,Google 
500,Cnn 
100,Yahoo 

'x'是一個矢量,那麼是什麼導致了這個錯誤呢?

+2

'x'是你稱爲'x $金額< - 因子(x $金額)'的一個因素,但應該是數字向量 – DrDom 2013-04-20 18:13:39

+0

@DDDom是的工作,將你的評論放入一個答案? – 2013-04-20 18:17:46

回答

2

刪除轉換因子x$Amount <- factor(x$Amount) 並作出小的變化在

dotchart(x$Amount, 
    labels = row.names(x), 
    cex=.7, 
    groups = factor(x$Amount), 
    gcolor = "black", 
    color = x$color, 
    pch=19, 
    main = "Gas Mileage for Car Models\ngrouped by cylinder", 
    xlab = "Miles Per Gallon") 

也許這將幫助你。

+0

你爲什麼使用'factor(x $ Amount)'而不是x $ Amount? – 2013-04-20 18:28:03

+0

從幫助頁面:「groups」 - 一個可選因子,指示x的元素如何分組。如果x是矩陣,則組將默認爲x的列。所以它應該是一個因素,我做了明確的轉換因子,但可能'dotchart'可以自動完成。 – DrDom 2013-04-20 18:33:35

+0

好的,謝謝澄清,結果標籤是不同的,當我省略它 – 2013-04-20 18:34:22