get_correlation: Get correlation value in a tidy way

View source: R/get_correlation.R

get_correlationR Documentation

Get correlation value in a tidy way

Description

Determine the Pearson correlation coefficient between two variables in a data frame using pipeable and formula-friendly syntax

Usage

get_correlation(data, formula, na.rm = FALSE, ...)

Arguments

data

a data frame object

formula

a formula with the response variable name on the left and the explanatory variable name on the right

na.rm

a logical value indicating whether NA values should be stripped before the computation proceeds.

...

further arguments passed to stats::cor()

Value

A 1x1 data frame storing the correlation value

Examples

library(moderndive)

# Compute correlation between mpg and cyl:
mtcars %>%
  get_correlation(formula = mpg ~ cyl)

# Group by one variable:
library(dplyr)
mtcars %>%
  group_by(am) %>%
  get_correlation(formula = mpg ~ cyl)

# Group by two variables:
mtcars %>%
  group_by(am, gear) %>%
  get_correlation(formula = mpg ~ cyl)

moderndive documentation built on Sept. 11, 2024, 9:26 p.m.