assign_portfolio {tidyfinance}R Documentation

Assign Portfolios Based on Sorting Variable

Description

[Experimental]

This function assigns data points to portfolios based on a specified sorting variable. It can optionally filter the data by exchanges before assignment. The function requires either the number of portfolios to be created or specific percentiles for the breakpoints, but not both.

Usage

assign_portfolio(
  data,
  sorting_variable,
  n_portfolios = NULL,
  percentiles = NULL,
  exchanges = NULL
)

Arguments

data

A data frame containing the dataset for portfolio assignment.

sorting_variable

A string specifying the column name in data to be used for sorting and determining portfolio assignments.

n_portfolios

An optional integer specifying the number of equally sized portfolios to create. This parameter is mutually exclusive with percentiles.

percentiles

An optional numeric vector specifying the percentiles for determining the breakpoints of the portfolios. This parameter is mutually exclusive with n_portfolios.

exchanges

An optional character vector specifying exchange names to filter the data before computing breakpoints and assigning portfolios. Exchanges must be stored in a column named exchange in data. If NULL, no filtering is applied.

Value

A vector of portfolio assignments for each row in the input data.

Note

This function will stop and throw an error if both n_portfolios and percentiles are provided or if neither is provided. Ensure that you only use one of these parameters for specifying portfolio breakpoints.

Examples

data <- data.frame(
  id = 1:100,
  exchange = sample(c("NYSE", "NASDAQ"), 100, replace = TRUE),
  market_cap = runif(100, 1e6, 1e9)
)
assign_portfolio(data, "market_cap", n_portfolios = 5)
assign_portfolio(data, "market_cap", percentiles = c(0.2, 0.4, 0.6, 0.8), exchanges = c("NYSE"))


[Package tidyfinance version 0.1.0 Index]