I'm trying to generate a corrplot with numbers in R and the following error appears: "Error in is.finite(tmp) : default method not implemented for type 'list'"
I tried transforming my list into data frame, but for sure I'm doing something wrong.
Could anyone help me to solve it?
Thanks in advance
Note: I put a picture showing the "x"
Below my code
library(corrplot)
x<-read.table("corrplot_2.txt")
x <- as.data.frame(x)
corrplot(x, method="number")
CodePudding user response:
The problem is that the input of corrplot
should be a correlation matrix:
The correlation matrix to visualize, must be square if order is not 'original'. For general matrix, please using is.corr = FALSE to convert.
So you first have to convert your dataframe to a correlation matrix using cor
like this:
library(corrplot)
x<-read.table("corrplot_2.txt")
x <- as.data.frame(x)
M <- cor(x)
corrplot(M, method="number")
CodePudding user response:
Problem
You receive this error because is.finite()
cannot handle data frames / lists:
# example
df <- as.data.frame(matrix(runif(30), ncol=5))
is.finite(df)
> is.finite(df)
Error in is.finite(df) : default method not implemented for type 'list'
Solution
The functions lapply()
or sapply()
allow us to use is.finite()
for every column:
# solution 1
lapply(df, is.finite)
sapply(df, is.finite)
> lapply(df, is.finite)
$V1
[1] TRUE TRUE TRUE TRUE TRUE TRUE
$V2
[1] TRUE TRUE TRUE TRUE TRUE TRUE
...
> sapply(df, is.finite)
V1 V2 V3 V4 V5
[1,] TRUE TRUE TRUE TRUE TRUE
[2,] TRUE TRUE TRUE TRUE TRUE
...
Since we probably don't want an answer for each value individually, but we want to know if all of them are finite, we add an all()
:
# solution 2
all(sapply(df, is.finite))
> all(sapply(df, is.finite))
[1] TRUE