2017-04-12 417 views
2

我想從csv文件讀取數據,將其保存爲矩陣並將其用於可視化。當使用從csv文件讀取的矩陣使用corplot函數時,'dimnames'[2]的長度不等於數組範圍

data<-read.table("Desktop/Decision_Tree/cor_test_.csv",header = F,sep = ",") 

data 
V1 V2 V3 V4 V5  V6 
1 1.00 0.00 0.00 0.00 0.00 0 
2 0.11 1.00 0.00 0.00 0.00 0 
3 0.12 0.03 1.00 0.00 0.00 0 
4 -0.04 0.54 0.32 1.00 0.00 0 
5 -0.12 0.57 -0.09 0.26 1.00 0 
6 0.21 -0.04 0.24 0.18 -0.21 1 

它進展順利。但隨後:

corrplot(data, method = 'color', addCoef.col="grey") 

據說:

錯誤矩陣(不公開(值,遞歸= FALSE,use.names = FALSE),nrow = NR,: 長度的「dimnames 「[2]不等於陣列程度

不知如何解決這個問題。

回答

6

corrplot需要一個矩陣,我想你的數據是一個數據幀,使用改爲。

實施例:

## Your data as data frame: 
data <- structure(list(V1 = c(1, 0.11, 0.12, -0.04, -0.12, 0.21), V2 = c(0, 
        1, 0.03, 0.54, 0.57, -0.04), V3 = c(0, 0, 1, 0.32, -0.09, 0.24 
       ), V4 = c(0, 0, 0, 1, 0.26, 0.18), V5 = c(0, 0, 0, 0, 1, -0.21 
       ), V6 = c(0, 0, 0, 0, 0, 1)), .Names = c("V1", "V2", "V3", "V4", 
       "V5", "V6"), row.names = c(NA, -6L), class = "data.frame") 

## Using the data frame results in an error: 
corrplot::corrplot(data, method = 'color', addCoef.col = "grey") 
# Error in matrix(unlist(value, recursive = FALSE, use.names = FALSE), nrow = nr, : 
# length of 'dimnames' [2] not equal to array extent 

## Using the matrix works: 
corrplot::corrplot(as.matrix(data), method = 'color', addCoef.col = "grey") 

corrplot

+0

Reeeeeally理解爲溶液。它完美的作品。多謝。 –