% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/assign_portfolio.R
\name{assign_portfolio}
\alias{assign_portfolio}
\title{Assign Portfolios Based on Sorting Variable}
\usage{
assign_portfolio(
  data,
  sorting_variable,
  breakpoint_options = NULL,
  breakpoint_function = compute_breakpoints,
  data_options = NULL
)
}
\arguments{
\item{data}{A data frame containing the dataset for portfolio assignment.}

\item{sorting_variable}{A string specifying the column name in \code{data} to be
used for sorting and determining portfolio assignments based on the
breakpoints.}

\item{breakpoint_options}{An optional named list of arguments passed to
\code{breakpoint_function}.}

\item{breakpoint_function}{A function to compute breakpoints. The default
is set to \code{\link[=compute_breakpoints]{compute_breakpoints()}}.}

\item{data_options}{A list of class \code{tidyfinance_data_options} (created via
\code{\link[=data_options]{data_options()}}) specifying column name mappings. Passed through to
\code{breakpoint_function}. When using the default \code{\link[=compute_breakpoints]{compute_breakpoints()}},
the \code{exchange} element is used to specify the exchange column, and
\code{mktcap_lag} is used to specify the market capitalization column. Uses
\code{\link[=data_options]{data_options()}} default if \code{NULL}: \code{"exchange" = "exchange"} and
\code{"mktcap_lag" = "mktcap_lag"}.}
}
\value{
A vector of integer portfolio assignments for each row in the
input \code{data}.
}
\description{
\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#experimental}{\figure{lifecycle-experimental.svg}{options: alt='[Experimental]'}}}{\strong{[Experimental]}}

Assigns data points to portfolios based on a specified sorting variable and
the selected function to compute breakpoints. Users can specify a function
to compute breakpoints. The function must take \code{data} and
\code{sorting_variable} as the first two arguments. Additional arguments are
passed with a named list \code{\link[=breakpoint_options]{breakpoint_options()}}. The function needs to
return an ascending vector of breakpoints. By default, breakpoints are
computed with \code{\link[=compute_breakpoints]{compute_breakpoints()}}. The default column names can be
modified using \code{\link[=data_options]{data_options()}}.
}
\examples{
set.seed(42)
data <- data.frame(
  id = 1:100,
  exchange = sample(c("NYSE", "NASDAQ"), 100, replace = TRUE),
  market_cap = 1:100
)

assign_portfolio(data, "market_cap", breakpoint_options(n_portfolios = 5))

assign_portfolio(
  data,
  "market_cap",
  breakpoint_options(
    percentiles = c(0.2, 0.4, 0.6, 0.8),
    breakpoints_exchanges = c("NYSE")
  )
)

}
\seealso{
Other portfolio functions: 
\code{\link{breakpoint_options}()},
\code{\link{compute_breakpoints}()},
\code{\link{compute_long_short_returns}()},
\code{\link{compute_portfolio_returns}()},
\code{\link{data_options}()},
\code{\link{filter_options}()},
\code{\link{filter_sorting_data}()},
\code{\link{implement_portfolio_sort}()},
\code{\link{portfolio_sort_options}()}
}
\concept{portfolio functions}
