2014-01-26 95 views
1

我的數據位於1列x 40000行數據幀中。我想使它成爲200列x 200行數據框。當我使用所有數值時,我使用下面的代碼將我的矢量轉換爲矩陣。R中的1D數據幀轉換爲2D格式

spematrix=data.matrix(spe) 
matrix = matrix(spematrix, nrow = 200, ncol=200) 

我現在不能使用它,因爲我的數據包含非數值(用逗號分隔的數字)。

我是R的新手,很感激任何幫助。

+0

歡迎您!通常情況下,我們不會在問題中放置感謝信息,因爲您可以在這裏閱讀:「[應該'嗨','謝謝',標語和祝福從帖子中刪除?](http://meta.stackexchange.com/questions/ 2950/should-hi-thanks-taglines-and-salutations-be-from-posts)「 – veducm

+1

+1謝謝。我認爲不應該暫緩禮節。 –

回答

2

聽起來像你已經知道該怎麼做。您可以gsub擦洗你的數據刪除,字符:

spe <- gsub(",","",spe[,1]) # returns a character vector 
spe <- as.numeric(spe) # convert to numeric 

然後,

spematrix <- data.matrix(spe) matrix = matrix(spematrix, nrow = 200, ncol=200)