
Maps values into labeled bins from jefferys/FusionExpressionPlot by Stuart R. Jefferys Basically this is a convenience wrapper around cut.
mapLabels.RdMaps labels to a vector of numeric data based on a vector of bin ends. Each
label corresponds to one of n bins defined by n+1 bin boundaries (including
start and end boundaries, probably -Inf and Inf). It is an
error if the number of bins and the number of labels differ. Returns a vector
of the same length as data, but with each value replaced by the binLabel for
the bin it was sorted into. E.g if the bin labels are categories like "low",
"medium" and "high", this converts data to categories. Values equal to bin
boundaries are put in the lower bin, with the lowest bin boundary also
included in the lowest bin. Note, bin ends are always sorted from lowest to
highest. If you want the labels in the other order, just reverse the labels
vector.
Arguments
- data
A vector of numeric data to bin. Where values are
NA, above the highest bin end, or below the lowest they convert toNAin the returned label vector.- binEnds
The n+1 ends of the n bins into which the data is sorted. One bin might be
c(-Inf, Inf). Two bins equally weighted would bec(-Inf, median(data), Inf). This vector will be sorted before assigning labels, so reverse the label order instead of providing bins as high to low. Data exactly matching bin ends will be in the lower bin, with the lowest bin end also part of the lowest bin. It is an error if any bin end is repeated.- binLabels
The vector of names corresponding to the labels of the bin a value belongs in.
Errors
-
'breaks' are not unique It is an error if any bin end is repeated.
-
lengths of 'breaks' and 'labels' differ It is an error if the number of labels differs from the number of bins, or equivalently is not one less than the number of bin ends.
Examples
ends = c(Inf,-Inf,1,-1) # Sorted to c(-Inf, -1, 1, Inf)
labels = c('-', '0', '+' )
mapLabels( c(-Inf,-5,-1,-0.5,0,0.5,1,5,Inf), ends, labels)
#> [1] "-" "-" "-" "0" "0" "0" "0" "+" "+"
#=> [1] "-", "-", "-", "0", "0", "0", "0", "+", "+"
ends = c(-10, 0, 10)
labels = c('-', '+')
mapLabels( c(-20, -10, -5, 0, NA, 5.2, 10, Inf), ends, labels)
#> [1] NA "-" "-" "-" NA "+" "+" NA
#=> [1] NA "-" "-" "-" NA "+" "+" NA