nameVector: assign unique names for a vector

nameVectorR Documentation

assign unique names for a vector

Description

assign unique names for a vector

Usage

nameVector(x, y = NULL, makeNamesFunc = makeNames, ...)

Arguments

x

vector input, or data.frame, matrix, or tibble with two columns, the second column is used to name values in the first column.

y

NULL or character vector of names. If NULL then x is used. Note that y is recycled to the length of x, prior to being sent to the makeNamesFunc. In fringe cases, y can be a matrix, data.frame, or tibble, in which case pasteByRow will be used to create a character string to be used for vector names. Note this case is activated only when x is not a two column matrix, data.frame, or tibble.

makeNamesFunc

function to make names unique, by default makeNames which ensures names are unique.

...

passed to makeNamesFunc, or to pasteByRow if y is a two column data.frame, matrix, or tibble. Thus, sep can be defined here as a delimiter between column values.

Details

This function assigns unique names to a vector, if necessary it runs makeNames to create unique names. It differs from setNames in that it ensures names are unique, and when no names are supplied, it uses the vector itself to define names. It is helpful to run this function inside an lapply function call, which by default maintains names, but does not assign names if the input data did not already have them.

When used with a data.frame, it is particularly convenient to pull out a named vector of values. For example, log2 fold changes by gene, where the gene symbols are the name of the vector.

nameVector(genedata[,c("Gene","log2FC")])

Value

vector with names defined

See Also

Other jam string functions: asSize(), breaksByVector(), cPasteSU(), cPasteS(), cPasteUnique(), cPasteU(), cPaste(), fillBlanks(), formatInt(), gsubOrdered(), gsubs(), makeNames(), mixedOrder(), mixedSortDF(), mixedSorts(), mixedSort(), mmixedOrder(), nameVectorN(), padInteger(), padString(), pasteByRowOrdered(), pasteByRow(), sizeAsNum(), tcount(), ucfirst(), uniques()

Examples

# it generally just creates names from the vector values
nameVector(LETTERS[1:5]);

# if values are replicated, the makeNames() function makes them unique
V <- rep(LETTERS[1:5], each=3);
nameVector(V);

# for a two-column data.frame, it creates a named vector using
# the values in the first column, and names in the second column.
df <- data.frame(seq_along(V), V);
df;
nameVector(df);

# Lastly, admittedly a fringe case, it can take a multi-column data.frame
# to generate labels:
nameVector(V, df);


jmw86069/jamba documentation built on March 26, 2024, 5:26 a.m.