2014-07-27 42 views
0

我正在使用從該頁面例如重新排序:http://kohske.wordpress.com/2010/12/29/faq-how-to-order-the-factor-variables-in-ggplot2/但代碼似乎並不奏效:爲什麼因子變量不是R中

d <- data.frame(Team1=c("Cowboys", "Giants", "Eagles", "Redskins"), Win=c(20, 13, 9, 12)) 
d 
    Team1 Win 
1 Cowboys 20 
2 Giants 13 
3 Eagles 9 
4 Redskins 12 
> 
d$Team1 
[1] Cowboys Giants Eagles Redskins 
Levels: Cowboys Eagles Giants Redskins 
> 
> 
d$Team3 <- reorder(d$Team1, d$Win) 
d 
    Team1 Win Team3 
1 Cowboys 20 Cowboys 
2 Giants 13 Giants 
3 Eagles 9 Eagles 
4 Redskins 12 Redskins 
> 
> 
d$Team3 
[1] Cowboys Giants Eagles Redskins 
Levels: Cowboys Eagles Giants Redskins 
> 
> 
d[order(d$Team1),] 
    Team1 Win Team3 
1 Cowboys 20 Cowboys 
3 Eagles 9 Eagles 
2 Giants 13 Giants 
4 Redskins 12 Redskins 
> 
d[order(d$Team3),] 
    Team1 Win Team3 
1 Cowboys 20 Cowboys 
3 Eagles 9 Eagles 
2 Giants 13 Giants 
4 Redskins 12 Redskins 
> 

的Team3列是相同TEAM1儘管reoder命令。哪裏有問題。謝謝你的幫助。

編輯:

levels(d$Team1) 
[1] "Cowboys" "Eagles" "Giants" "Redskins" 
> 
levels(d$Team3) 
[1] "Cowboys" "Eagles" "Giants" "Redskins" 
> 
+0

當我運行代碼,我沒有得到'結果你正在顯示的「d $ Team3」。我看到水平重新排序。您似乎也在混合'order'(data.frame中變量的排序順序)和'reorder'(與變量中每個整數值對應的'levels')。也許這也造成了一些困惑。 – Thomas

+0

我正在使用命令來測試級別是否已重新排序。我嘗試重新啓動R,但問題依然存在。我在Linux Debian Stable和版本3.1.0窗口上使用R.version 2.15.1。 – rnso

回答

0

功能reorder改變了水平,值的不是訂單的訂單:

> levels(d$Team1) 
[1] "Cowboys" "Eagles" "Giants" "Redskins" 
> levels(d$Team3) 
[1] "Eagles" "Redskins" "Giants" "Cowboys" 
+0

我得到相同的輸出。請看我的編輯。 – rnso