2014-04-08 49 views
0

我似乎無法將列表轉換爲矩陣。我使用加載.csv文件:將列表轉換爲數字矩陣r

dat = read.csv("games.csv", header = TRUE) 

> typeof(dat) 
[1] "list" 

但是,當我嘗試將其轉換爲數字矩陣使用:

遊戲= data.matrix(DAT)

的條目'價值觀因一些原因而改變。問題是什麼?

+1

由於缺乏信息,這個問題有點不清楚。例如,你能描述dat的尺寸或結構嗎?當您在控制檯中運行「head(dat)」時,您是否能夠顯示一些示例輸出,以及運行遊戲時獲得的輸出= data.matrix(dat)行(也許您可以顯示頭部(遊戲) )?下面的答案是基於不完全信息的第一個響應。 –

+1

您應該提供[**最小,可重現的示例**](http://stackoverflow.com/questions/5963269/how-to-make-a-非常可再現的例子/ 5963610#5963610) – Henrik

回答

1

沒有任何其他信息所提供的,也許你可以試試:

games <- as.matrix(dat) 
2

雖然納撒尼爾的解決方案爲你工作,我想指出的是,你可能需要調整你的是怎麼回事的感覺是非常重要的。

typeof(dat)可能是list,但classdata.frame

這可能有助於說明的區別:

# typeof() & class() of `pts` is `list` 
# whereas typeof() `dat` in your example is `list` but 
# class() of `dat` in your example is a `data.frame` 

pts <- list(x = cars[,1], y = cars[,2]) 

as.matrix(pts) 
## [,1]  
## x Numeric,50 
#3 y Numeric,50 

head(as.matrix(data.frame(pts))) 
##  x y 
## [1,] 4 2 
## [2,] 4 10 
## [3,] 7 4 
## [4,] 7 22 
## [5,] 8 16 
## [6,] 9 10 

這是兩個基本上從「as.matrix()`函數不同的結果。

只要確保您不會對結果感到失望,如果您在read.csv以外的其他環境中嘗試此操作。

+0

謝謝,我不知道 – Roy