Learn R Programming

tidyquant (version 1.0.9)

excel_stat_summary_functions: Excel Statistical Summary Functions

Description

15+ common statistical functions familiar to users of Excel (e.g. SUM(), AVERAGE()). These functions return a single value (i.e. a vector of length 1).

These functions are designed to help users coming from an Excel background. Most functions replicate the behavior of Excel:

  • Names in most cases match Excel function names

  • Functionality replicates Excel

  • By default, missing values are ignored (same as in Excel)

Usage

SUM(x)

AVERAGE(x)

MEDIAN(x)

MIN(x)

MAX(x)

COUNT(x)

COUNT_UNIQUE(x)

STDEV(x)

VAR(x)

COR(x, y)

COV(x, y)

FIRST(x)

LAST(x)

NTH(x, n = 1)

CHANGE_FIRSTLAST(x)

PCT_CHANGE_FIRSTLAST(x)

Value

  • Summary functions return a single value

Arguments

x

A vector. Most functions are designed for numeric data. Some functions like COUNT() handle multiple data types.

y

A vector. Used in functions requiring 2 inputs.

n

A single value used in NTH() to select a specific element location to return.

Useful functions

Summary Functions - Return a single value from a vector

  • Sum: SUM()

  • Center: AVERAGE(), MEDIAN()

  • Spread: STDEV(), VAR()

  • Range: MIN(), MAX()

  • Count: COUNT(), COUNT_UNIQUE()

  • Position: FIRST(), LAST(), NTH()

  • Change (Summary): CHANGE_FIRSTLAST(), PCT_CHANGE_FIRSTLAST()

  • Correlation: COR(), COV()

Details

Summary Functions

  • All functions remove missing values (NA). This is the same behavior as in Excel and most commonly what is desired.

Examples

Run this code
# Libraries
library(timetk, exclude = "FANG")
library(forcats)
library(dplyr)

# --- Basic Usage ----

SUM(1:10)

PCT_CHANGE_FIRSTLAST(c(21, 24, 22, 25))

# --- Usage with tidyverse ---

# Go from daily to monthly periodicity,
# then calculate returns and growth of $1 USD
FANG %>%
    mutate(symbol = forcats::as_factor(symbol)) %>%
    group_by(symbol) %>%

    # Summarization - Collapse from daily to FIRST value by month
    summarise_by_time(
        .date_var  = date,
        .by        = "month",
        adjusted   = FIRST(adjusted)
    )

Run the code above in your browser using DataLab