Correlation Coefficient for 1 variable against multiple other variables

Hi all,
I am trying to calculate the correlation coefficient in a large set of data and can't quite get the mutate function to work.
This is an example of my data

Z       A         B
1.3   0.9    0.75
1.6   0.5     0.3
1.9   0.2     0.1

I am trying to calculate the coefficients for Z & A, Z & B.

example.data1 %>%
group_by(Z) %>%
nest() %>%
mutate(cor = map(data, ~cor(.x$A, .x$B))) %>%
unnest(cor, .drop = TRUE)

That was the code I am inputting however this is the result:

# A tibble: 3 x 3
# Groups:   Z [3]
      Z data               cor
  <dbl> <list>           <dbl>
1    13 <tibble [1 × 2]>    NA
2    16 <tibble [1 × 2]>    NA
3     9 <tibble [1 × 2]>    NA

Is it possible to get the correlations for A & B to appear in my console in this format instead of NA?

Hello there,

Welcome to the forum! I had a quick look at your code. I think the easiest solution would be to in fact run the correlation coefficient on the whole dataframe and just selecting the column you need (in this case keeping Z so we can see the relationship between Z & A and Z & B.

I had a look to see what your code was doing and the group_by doesn't seem to do much and the nest as well is creating a 3 x 2 dataframe which is likely also causing the problem as it typically wants a matrix of equal size (i.e 3 x 3).

Let me know if this helps?

library(tidyverse)
#> Warning: package 'ggplot2' was built under R version 3.6.2
#> Warning: package 'tidyr' was built under R version 3.6.2
#> Warning: package 'purrr' was built under R version 3.6.2
#> Warning: package 'dplyr' was built under R version 3.6.3

df <- data.frame(
           z = c(1.3, 1.6, 1.9),
           a = c(0.9, 0.5, 0.2),
           b = c(0.75, 0.3, 0.1)
)

df_output <- cor(df) %>% as.data.frame() %>%  select(z)

df_output
#>            z
#> z  1.0000000
#> a -0.9966159
#> b -0.9762210

output <- df %>% group_by(z) %>% nest()

output
#> # A tibble: 3 x 2
#> # Groups:   z [3]
#>       z data            
#>   <dbl> <list>          
#> 1   1.3 <tibble [1 x 2]>
#> 2   1.6 <tibble [1 x 2]>
#> 3   1.9 <tibble [1 x 2]>

Created on 2020-10-07 by the reprex package (v0.3.0)

Hi,

Thanks that worked! Appreciate the help!

@pc96, most welcome :slight_smile: Feel free to mark the post with the code as the solution

This topic was automatically closed 7 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.