2015-06-01 147 views
0

我在R中使用Sparklines軟件包,並使用reshapeExt()函數來準備數據集,但遇到了一個非常非常奇怪的問題。該數據集是這樣的:R Sparklines軟件包錯誤

 Company time value 
1 Microsoft 1990 11 
2 Time Warner 1990 22 
3  Verizon 1990 33 
4 Microsoft 1991 44 
5 Time Warner 1991 55 
6  Verizon 1991 66 
7 Microsoft 1992 77 
8 Time Warner 1992 88 
9  Verizon 1992 99 

然後我跟着教程示例運行這些行:

example <- example[,c("Company","value","time")] 
example$time <- as.numeric (as.character (example$ time)) 
dat <- reshapeExt (example , idvar =" Company " , varying = list (2)) 

令人費解的是,R給了我這個爲 「逸」:

 Company time value Company 
1 Microsoft 1 11   1 
2 Time Warner 1 22   2 
3  Verizon 1 33   3 
4 Microsoft 1 44   4 
5 Time Warner 1 55   5 
6  Verizon 1 66   6 
7 Microsoft 1 77   7 
8 Time Warner 1 88   8 
9  Verizon 1 99   9 

時間列全部變爲1,沒有明顯的原因。當我在https://web.warwick.ac.uk/statsdept/user2011/TalkSlides/Contributed/18Aug_0950_FocusVI_4-ReportingData_2-Kowarik.pdf第18頁實施示例時不會發生這種情況 - 此處發生了什麼?

任何幫助

回答

0

後8個月讚賞,我不知道這是否是對您有用,但是,我有一個類似的問題,我發現你的問題。無論如何,這可能會幫助其他人。

首先,證明你有這個順序只有3列: 公司,時間,價值

我認爲這個問題是因爲數據的順序,你需要通過公司的時間訂購:

example <- examplewith(example, order(Company, time)), ] 

那麼數據會是這樣:

 Company time value 
1 Microsoft 1990 11 
4 Microsoft 1991 44 
7 Microsoft 1992 77 
2 Time_Warner 1990 22 
5 Time_Warner 1991 55 
8 Time_Warner 1992 88 
3  Verizon 1990 33 
6  Verizon 1991 66 
9  Verizon 1992 99 

然後使用reshapeExt,並更改varying = list(3) 3,因爲你的值在第三列。

example2 <- reshapeExt(example, idvar = "Company", varying = list(3)) 

最終的結果是:

 Company time value 
1 Microsoft 1 11 
4 Microsoft 2 44 
7 Microsoft 3 77 
2 Time_Warner 1 22 
5 Time_Warner 2 55 
8 Time_Warner 3 88 
3  Verizon 1 33 
6  Verizon 2 66 
9  Verizon 3 99 

我想會以這種方式工作。 謝謝!