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.
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]>