Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: default method not implemented for type 'list' in corrplot in R version 4.1.2

Tags:

r

enter image description here

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")
like image 240
Rafaela Avatar asked Mar 11 '26 17:03

Rafaela


2 Answers

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")
like image 85
Quinten Avatar answered Mar 14 '26 09:03

Quinten


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
like image 33
Caspar V. Avatar answered Mar 14 '26 09:03

Caspar V.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!