当使用从csv文件读取的矩阵中的corrplot函数时,"dimnames" [2]的长度不等于数组范围 [英] length of 'dimnames' [2] not equal to array extent when using corrplot function from a matrix read from a csv file

查看:827
本文介绍了当使用从csv文件读取的矩阵中的corrplot函数时,"dimnames" [2]的长度不等于数组范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从csv文件中读取数据,将其另存为矩阵并用于可视化.

I wanna read the data from a csv file, save it as a matrix and use it for visualization.

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")

据说:

矩阵中的错误(unlist(value,递归= FALSE,use.names = FALSE),nrow = nr,: 'dimnames'[2]的长度不等于数组范围

Error in matrix(unlist(value, recursive = FALSE, use.names = FALSE), nrow = nr, : length of 'dimnames' [2] not equal to array extent

我不知道该怎么解决.

推荐答案

corrplot需要一个矩阵,我假设您的数据是一个数据帧.改为使用as.matrix(data).

corrplot requires a matrix, I assume your data is a data frame. Use as.matrix(data) instead.

示例:

## 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")

这篇关于当使用从csv文件读取的矩阵中的corrplot函数时,"dimnames" [2]的长度不等于数组范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆