Filtering correlation matrix in R

Hello guys

I am undergoing trouble to extract the data from a correlation matrix, I'm trying to extract values that are conditions like ex) (p < 0.05, |r| > 0.5) .
I did p value filtering by "conf.level = 0.95" this command.

However, i could not catch how to filtering correlation "r" value.


library(dplyr)
library(tibble)
library(tidyr)
library(magrittr)
library(corrplot)
library(corrr)
library(RColorBrewer)

my_data <- read.csv("raw_data/kwon.csv")

bacteria <- read.csv("raw_data/lee.csv")

row.names(bacteria) <- LETTERS[1:3]

has_rownames(bacteria)

row.names(my_data) <- LETTERS[1:3]

has_rownames(my_data)

library(RColorBrewer)
df <- bind_cols(bacteria[, -1],my_data[, -1])

res1 <- cor.mtest(df, conf.level = 0.95)
res2 <- res1$p %>% as.data.frame()  
rownames(res2) <- colnames(df)
colnames(res2) <- colnames(df)
res2 <- res2 %>%
  rownames_to_column("rowname") %>%
  gather("var", "p", -rowname)

corrr::correlate(bind_cols(my_data[, -1], bacteria[, -1])) %>%
  pivot_longer(cols=-term,names_to="var",values_to = 'value') %>%
  rename(rowname = term) %>%
  left_join(res2) %>%
  mutate(value = ifelse(p < 0.05, NA_integer_, value)) %>%
  select(-p) %>%
  spread(var, value) %>%
  filter(rowname %in% colnames(my_data)) %>%
  select(one_of(c("rowname", colnames(bacteria)[-1]))) %>%
  as.data.frame() %>%
  column_to_rownames("rowname") %>%
  as.matrix() %>%
  corrplot::corrplot(is.corr = FALSE, na.label.col = "white", 
                     col = brewer.pal (n=10, name= "PuOr"), tl.col = "black", tl.srt = 45)

This my files


kwon <- tibble::tribble(
    ~NAME,    ~A,    ~B,    ~C,    ~D,   ~E,   ~F,
  "CON-1", 35.42, 63.07, 19.13,  17.8,  6.5, 24.8,
  "CON-2", 34.63, 64.22, 19.08,  16.7,  6.5, 24.8,
  "CON-3", 34.04,    64, 18.87, 17.12, 6.48, 24.6
  )
head(kwon)
#> # A tibble: 3 x 7
#>   NAME      A     B     C     D     E     F
#>   <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 CON-1  35.4  63.1  19.1  17.8  6.5   24.8
#> 2 CON-2  34.6  64.2  19.1  16.7  6.5   24.8
#> 3 CON-3  34.0  64    18.9  17.1  6.48  24.6


lee <- tibble::tribble(
    ~NAME,  ~A1, ~A2,  ~A3, ~A4,  ~A5,   ~A6, ~A7,
  "CON-1", 130L, 12L, 118L, 33L, 497L, 1951L, 85L,
  "CON-2",  89L, 20L, 156L, 16L, 560L, 2338L, 87L,
  "CON-3", 135L,  0L, 148L, 31L, 604L, 2676L, 68L
  )
head(lee)
#> # A tibble: 3 x 8
#>   NAME     A1    A2    A3    A4    A5    A6    A7
#>   <chr> <int> <int> <int> <int> <int> <int> <int>
#> 1 CON-1   130    12   118    33   497  1951    85
#> 2 CON-2    89    20   156    16   560  2338    87
#> 3 CON-3   135     0   148    31   604  2676    68```


Please, if you find any mistake here, let me know experts.

Hello,
I'm sure you shared this image with the best intentions, but perhaps you didnt realise what it implies.
If someone wished to use example data to test code against, they would type it out from your screenshot...

This is very unlikely to happen, and so it reduces the likelihood you will receive the help you desire.
Therefore please see this guide on how to reprex data. Key to this is use of either datapasta, or dput() to share your data as code

2 Likes

I appreciate your comment. :grinning:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.