How I can calculate the variance across all columns in a data frame in R according to the values of another data frame using Dplyr?

Viewed 33

I have a table (table1) with correlation coefficients that correspond to a variable as described below :

var = c("A","B","C","D","E")
cor = c(0.7,0.3,0.5,0.1,0.9) 
table1 = tibble(var,cor)
# A tibble: 5 × 2
  var     cor
  <chr> <dbl>
1 A       0.7
2 B       0.3
3 C       0.5
4 D       0.1
5 E       0.9

I have a vector of interest :

y=c(1,2,3,4)

and a new table (table2) as shown below

A = c(1,2,NA,4)
B =c(5,6,7,8)
C=c(NA,10,11,12)
D=c(13,14,15,16)
table2 = tibble(A,B,C,D);table2
# A tibble: 4 × 4
      A     B     C     D
  <dbl> <dbl> <dbl> <dbl>
1     1     5    NA    13
2     2     6    10    14
3    NA     7    11    15
4     4     8    12    16

I want to calculate the covariance of vector y with (across) all columns of table2 but only if the corresponding correlation of table 1 is greater than 0.3 and if is less than 0.3 to return 0. Therefore I want to search for correlation in table 1 > 0.3 i.e A and C (because table 2 does not have column E). How I can implement this in R using base or dplyr package ?

1 Answers

This should work for you:

library(dplyr)
library(tidyr)

var = c("A","B","C","D","E")
cor = c(0.7,0.3,0.5,0.1,0.9) 
table1 = tibble(var,cor)

A = c(1,2,NA,4)
B =c(5,6,7,8)
C=c(NA,10,11,12)
D=c(13,14,15,16)
table2 = tibble(A,B,C,D)
table2

y=c(1,2,3,4)

table3 <- table2 %>% 
  summarise(across(.cols = everything(), cov, y = y, use = "complete.obs")) %>% 
  pivot_longer(cols = everything(), names_to = "var", values_to = "covar") %>% 
  merge(table1) %>% 
  filter(cor > 0.3)
Related