% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/select.random.R
\name{select.random}
\alias{select.random}
\title{Selection of Entries from Clusters/Groups by Random Sampling}
\usage{
select.random(data, names, group, alloc, always.selected = NULL)
}
\arguments{
\item{data}{The data as a data frame object. The data frame should possess
one row per individual and columns with the individual names and multiple
trait/character data.}

\item{names}{Name of column with the accession names as a character string.}

\item{group}{Name of column with the accession group/cluster names as a
character string.}

\item{alloc}{A named numeric vector specifying the number of entries to be
selected. Names should correspond to the levels of the "\code{"group"}
column, and values indicate the number of elements to be selected from each
level.}

\item{always.selected}{Names of accessions to be always included in the core
set as a character vector.}
}
\value{
A named list where each element contains the selected entry
  identifiers for a cluster/group.
}
\description{
Select entries from cluster/groups in the entire collection by random
sampling according to allocation specified.
}
\details{
For each cluster/group entries are selected randomly according to the
allocation provided
\insertCite{brown_core_1989,van_hintum_core_2000}{SampleCore}. Entries listed
as \code{always.selected} are mandatorily included in the selection. Warnings
are issued if requested allocation is smaller than the number of
always-selected entries in a cluster/group and/or when the cluster/group does
not contain enough remaining entries to fulfill the allocation.
}
\examples{
\dontshow{if (requireNamespace("cluster", quietly = TRUE)) withAutoprint(\{ # examplesIf}

library(cluster)

# Get data
data(cassava_EC_gp)

set.seed(123)
cassava_EC_gp <- cassava_EC_gp[sample(1:nrow(cassava_EC_gp), 500), ]

data <- cbind(genotypes = rownames(cassava_EC_gp), cassava_EC_gp)
row.names(data) <- NULL

# Prepare inputs
counts <- c(I = 31, II = 31, III = 18, IV = 35, V = 40, VI = 17)

mand_accns <-
  c("TMe-2018", "TMe-801", "TMe-3191", "TMe-1830", "TMe-1790")

# Specify the seed
set.seed(123)

# Fetch selected accessions
sel_random_out <-
  select.random(data = data, names = "genotypes",
                group = "Cluster", alloc = counts,
                always.selected = mand_accns)

sel_random_out

# Get distance matrix - Only for visualization
quant <- c("NMSR", "TTRN", "TFWSR", "TTRW", "TFWSS", "TTSW", "TTPW",
           "AVPW", "ARSR", "SRDM")
qual <- c("CUAL", "LNGS", "PTLC", "DSTA", "LFRT", "LBTEF", "CBTR", "NMLB",
          "ANGB", "CUAL9M", "LVC9M", "TNPR9M", "PL9M", "STRP", "STRC",
          "PSTR")

# Convert qualitative data columns to factor
cassava_EC_gp[, qual] <- lapply(cassava_EC_gp[, qual], as.factor)

# Standardise quantitative data column
cassava_EC_gp[, quant] <- lapply(cassava_EC_gp[, quant], function(x) {
  scale(x)[, 1]
})

gp_vec <- setNames(as.character(data[, "Cluster"]), data[, "genotypes"])

# Get the Gower's distance matrix
dist_matrix <- daisy(x = cassava_EC_gp[, c(qual, quant)],
                     metric = "gower")

plot_dist(d = dist_matrix, method = "isomds",
          gp = gp_vec,
          highlight =  unlist(sel_random_out, use.names = FALSE))
\dontshow{\}) # examplesIf}
}
\references{
\insertAllCited
}
\seealso{
\code{\link[SampleCore]{select.distance}},
  \code{\link[SampleCore]{select.diversity}}
}
