Find the shortest abbrevation to retain unique values
Source:R/jam_abbrevations.R
shortest_unique_abbreviation.RdFind the shortest abbrevation to retain unique values
Arguments
- x
charactervector- retain_contig_numbers
logical, defaultTRUE, whether numbers at the end of an abbreviated string should remain contiguous.When
TRUE, the goal is not to split a numeric value in the middle of the number.When
FALSEthe string will be abbreviated at the first position of uniqueness.
- ...
additional arguments are ignored.
Value
character vector named using unique values in x, and
whose values are the shortest abbreviated substrings which
maintain consistent uniqueness.
Details
This function is intended to abbreviate factor levels used in
statistical contrasts to the smallest substring that uniquely
represents the unique entries provided in x.
For example, c("one", "two", "three", "four") would be converted
to c("on", "tw", "th", "fo").
The default retain_contig_numbers=TRUE will attempt to retain
numeric values at the end of a string, to avoid splitting the number
at an intermediate position. This option only applies when the
character substring is not already unique before encountering
the numeric substring.
c("a", "p6", "p12", "p21")
the output keeps the contiguous numbers together:
c("a", "p6", "p12", "p21")
For this input:
c("a", "b6", "c12", "d21")only the first character is retained, because it is already unique:c("a", "b", "c", "d")
Todo
Consider some method to retain contiguous numbers at the end of a long string, while abbreviating the long string.
For this input:
c("adult", "prenatal6", "prenatal12", "prenatal21")the ideal output would be:c("a", "p6", "p12", "p21")To be fair, I do not know how to describe this logic. It may required breaking into words by character/non-character breakpoints, then applying substring to each?
See also
Other jamses utilities:
choose_annotation_colnames(),
combine_sestats(),
contrast2comp_dev(),
fold_to_log2fold(),
intercalate(),
list2im_opt(),
list2im_value_internal(),
list_to_sestats(),
log2fold_to_fold(),
make_block_arrow_polygon(),
mark_stat_hits(),
matrix_normalize(),
merge_statdf_all_test(),
point_handedness(),
point_slope_intercept(),
shrinkDataFrame(),
shrink_df(),
shrink_matrix(),
sort_samples(),
strsplitOrdered(),
sub_split_vector(),
update_function_params(),
update_list_elements()
Examples
x <- c("a", "p6", "p12", "p21");
shortest_unique_abbreviation(x)
#> a p6 p12 p21
#> "a" "p6" "p12" "p21"
shortest_unique_abbreviation(x, retain_contig_numbers=TRUE)
#> a p6 p12 p21
#> "a" "p6" "p12" "p21"
x1 <- c("male", "female");
shortest_unique_abbreviation(x1)
#> male female
#> "m" "f"
x2 <- c("Control", "Nicotine");
shortest_unique_abbreviation(x2)
#> Control Nicotine
#> "C" "N"
x3 <- c("Control", "Nicotine10", "Nicotine12", "Nicotine20");
shortest_unique_abbreviation(x3)
#> Control Nicotine10 Nicotine12 Nicotine20
#> "Control" "Nicotine10" "Nicotine12" "Nicotine20"
x4 <- c("one", "two", "three", "four");
shortest_unique_abbreviation(x4)
#> one two three four
#> "on" "tw" "th" "fo"