Apply unique to each element of a list, usually a list of vectors

uniques(
  x,
  keepNames = TRUE,
  incomparables = FALSE,
  useBioc = TRUE,
  useSimpleBioc = FALSE,
  xclass = NULL,
  ...
)

Arguments

x

input list of vectors

keepNames

boolean indicating whether to keep the list element names in the returned results.

incomparables

see unique() for details, this value is only sent to S4Vectors::unique() when the Bioconductor package S4Vectors is installed, and is ignored otherwise for efficiency.

useBioc

boolean indicating whether this function should try to use S4Vectors::unique() when the Bioconductor package S4Vectors is installed, otherwise it will use a somewhat less efficient bulk operation.

Value

list with unique values in each list element.

Details

This function will attempt to use S4Vectors::unique() which is substantially faster than any apply family function, especially for very long lists. However, when S4Vectors is not installed, it applies uniqueness to the unlisted vector of values, which is also substantially faster than the apply family functions for long lists, but which may still be less efficient than the C implementation provided by S4Vectors.

Examples

L1 <- list(CA=nameVector(LETTERS[c(1:4,2,7,4,6)]),
   B=letters[c(7:11,9,3)],
   D=nameVector(LETTERS[4]));
L1;
#> $CA
#>    A B_v1    C D_v1 B_v2    G D_v2    F 
#>  "A"  "B"  "C"  "D"  "B"  "G"  "D"  "F" 
#> 
#> $B
#> [1] "g" "h" "i" "j" "k" "i" "c"
#> 
#> $D
#>   D 
#> "D" 
#> 
uniques(L1);
#> $CA
#>    A B_v1    C D_v1    G    F 
#>  "A"  "B"  "C"  "D"  "G"  "F" 
#> 
#> $B
#>                         
#> "g" "h" "i" "j" "k" "c" 
#> 
#> $D
#>   D 
#> "D" 
#> 

if (1 == 1) {
if (suppressWarnings(suppressPackageStartupMessages(require(IRanges)))) {
   printDebug("Bioc CompressedList:");
   print(system.time(uniques(rep(L1, 10000), useBioc=TRUE)));
}
if (suppressWarnings(suppressPackageStartupMessages(require(S4Vectors)))) {
   printDebug("Bioc SimpleList:");
   print(system.time(uniques(rep(L1, 10000), useSimpleBioc=TRUE)));
}
printDebug("Simple list, keepNames=FALSE:");
print(system.time(uniques(rep(L1, 10000), useBioc=FALSE, keepNames=FALSE)));
printDebug("Simple list, keepNames=TRUE:");
print(system.time(uniques(rep(L1, 10000), useBioc=FALSE, keepNames=TRUE)));
}
#> ##  (19:07:53) 10Aug2023:   Bioc CompressedList: 
#>    user  system elapsed 
#>   0.032   0.001   0.034 
#> ##  (19:07:53) 10Aug2023:   Bioc SimpleList: 
#>    user  system elapsed 
#>   0.212   0.006   0.218 
#> ##  (19:07:53) 10Aug2023:   Simple list, keepNames=FALSE: 
#>    user  system elapsed 
#>   0.032   0.000   0.033 
#> ##  (19:07:54) 10Aug2023:   Simple list, keepNames=TRUE: 
#>    user  system elapsed 
#>   0.395   0.008   0.403