nameVector(x, y =NULL, makeNamesFunc = makeNames,...)
Arguments
x: character vector, or data.frame or equivalent (matrix, or tibble) with two columns, the second column is used to name values in the first column.
y: character or NULL, with 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.
Returns
vector with names defined
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")])
Examples
# it generally just creates names from the vector valuesnameVector(LETTERS[1:5]);# if values are replicated, the makeNames() function makes them uniqueV <- 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);
See Also
Other jam string functions: asSize(), breaksByVector(), fillBlanks(), formatInt(), gsubOrdered(), gsubs(), makeNames(), nameVectorN(), padInteger(), padString(), pasteByRow(), pasteByRowOrdered(), sizeAsNum(), tcount(), ucfirst()