79135179

Date: 2024-10-28 21:45:57
Score: 0.5
Natty:
Report link
set.seed(123)
data <- matrix(runif(100, min = 0, max = 1), nrow = 10)
data[, 1] <- 0   
data[, 5] <- 0   
data[, 7] <- 0 
data[, 8] <- 0 
df <- as.data.frame(data)
correlation_matrix <- cor(df)
correlation_matrix <- (correlation_matrix + 1) / 2
correlation_matrix[is.na(correlation_matrix)] <- 0
correlation_matrix

# Identify rows and columns with non-zero values (excluding the diagonal)
ix <- rowSums(correlation_matrix- diag(diag(correlation_matrix))) != 0; correlation_matrix[ix, ix]

# Subset the correlation matrix using ix
correlation_subset <- correlation_matrix[ix, ix]

# Ensure the subset is of class "matrix" for compatibility with corrplot
correlation_subset <- as.matrix(correlation_subset)

# Plot using corrplot with coefficients displayed
library(corrplot)
corrplot(correlation_subset, method = "color",addCoef.col = "black")

enter image description here

Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Davyd Antoniuk