gpt4 book ai didi

r - 使用 corrplot 时出错

转载 作者:行者123 更新时间:2023-12-03 21:04:59 25 4
gpt4 key购买 nike

我需要帮助解释使用 corrplot 的错误消息。

这是我的脚本

install.packages("ggplot2")
install.packages("corrplot")
install.packages("xlsx")

library(ggplot2)
library(corrplot)
library(xlsx)

#set working dir
setwd("C:/R")
#read xlsx data into R
df <- read.xlsx("TP_diff_frame.xlsx",1)
#set column as index
rownames(df) <- df$country
#remove column
df2<-subset(df, select = -c(country) )
#round values to to decimals


corrplot(df2, method="shade",shade.col=NA, tl.col="black", tl.srt=45)

我的df2:
> df2
a b c d e f g
Sweden 0.09 0.19 0.00 -0.25 -0.04 0.01 0.00
Germany 0.11 0.19 0.01 -0.35 0.01 0.02 0.01
UnitedKingdom 0.14 0.21 0.03 -0.32 -0.05 0.00 0.00
RussianFederation 0.30 0.26 -0.07 -0.41 -0.09 0.00 0.00
Netherlands 0.09 0.16 -0.05 -0.26 0.02 0.02 0.01
Belgium 0.12 0.20 0.01 -0.34 0.01 0.00 0.00
Italy 0.14 0.22 0.01 -0.37 0.00 0.00 0.00
France 0.14 0.24 -0.04 -0.34 0.00 0.00 0.00
Finland 0.16 0.17 0.01 -0.26 -0.08 0.00 0.00
Norway 0.15 0.21 0.10 -0.37 -0.09 0.00 0.00

和错误信息:
> corrplot(df2, method="shade",shade.col=NA, tl.col="black", tl.srt=45)

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

最佳答案

我认为问题在于您正在绘制数据框而不是相关矩阵。尝试将最后一行更改为:

corrplot(cor(df2), method="shade",shade.col=NA, tl.col="black", tl.srt=45)

函数 cor 计算相关矩阵,这是您需要绘制的

关于r - 使用 corrplot 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21140798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com