Efficiently fit correlation coefficient for matrix or two vectors

corr(x, y = NULL, spearman = FALSE)

Arguments

x

a matrix or vector

y

a vector. Optional.

spearman

Logical. Use Spearman's correlation?

Value

a numeric vector, the correlation coefficient

Examples

library(dplyr)
#> 
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#> 
#>     filter, lag
#> The following objects are masked from ‘package:base’:
#> 
#>     intersect, setdiff, setequal, union
# fit for entire data set
iris %>%
  select_if(is.numeric) %>%
  corr()
#>              Sepal.Length Sepal.Width Petal.Length Petal.Width
#> Sepal.Length    1.0000000  -0.1175698    0.8717538   0.8179411
#> Sepal.Width    -0.1175698   1.0000000   -0.4284401  -0.3661259
#> Petal.Length    0.8717538  -0.4284401    1.0000000   0.9628654
#> Petal.Width     0.8179411  -0.3661259    0.9628654   1.0000000

# just fit for two vectors
corr(iris$Sepal.Length, iris$Sepal.Width)
#> [1] -0.1175698