Optimized conversion of list to a value incidence matrix

list2im_value(
  setlist,
  empty = NULL,
  do_sparse = FALSE,
  force_sign = FALSE,
  ...
)

Arguments

setlist

list of vectors

empty

default single value used for empty/missing entries, the default empty=0 uses zero for entries not present. Another alternative is NA.

do_sparse

logical indicating whether to coerce the output to sparse matrix class "ngCMatrix" from the Matrix package. The default is FALSE.

...

additional arguments are ignored.

coerce_sign

logical indicating whether to coerce numeric vector values to the sign. When coerce_sign=FALSE the vector values are stored directly. When coerce_sign=TRUE the signs of the vector values are stored.

Value

Matrix object that contains signed direction encoded as c(-1, 0, 1) values.

Details

This function converts a list of named vectors into an incidence matrix with value for each entry (row) present in each input list (column). This output is called a "value incidence matrix" because the value itself is included in the matrix as opposed to a true incidence matrix that represents only TRUE or FALSE (1 or 0) at each position.

The rownames of the output matrix represent items, encoded by the vector names. The colnames of the output matrix represent the list names.

The default value in the output matrix is 0 for a numeric matrix, and "" for a character matrix, based on the input vector classes.

To override this behavior, use the argument empty. For example, it may be useful to encode missing entries as NA, which means "this entry was not observed", and store true values of 0 to indicate "this entry was observed and its value was 0 zero". In this case use empty=NA.

This behavior can be useful with gene expression data, when a particular gene may not be observed in all data sets.

See also

Examples

setlist <- make_venn_test(100, 3, do_signed=TRUE)
imv <- list2im_value(setlist);
print(head(imv));
#>          set_A set_B set_C
#> item_067    -1     0     0
#> item_042     1     0     0
#> item_050     1     0    -1
#> item_043    -1     0     0
#> item_014    -1     0     0
#> item_025     1    -1     0

# convert back to list
im_value2list(imv);
#> $set_A
#> item_067 item_042 item_050 item_043 item_014 item_025 item_090 item_091 
#>       -1        1        1       -1       -1        1       -1       -1 
#> item_069 item_093 item_057 item_009 item_072 item_026 item_007 item_099 
#>       -1        1        1       -1        1        1       -1       -1 
#> item_089 item_083 item_036 item_078 item_097 item_076 item_015 item_032 
#>       -1        1        1        1        1        1       -1        1 
#> item_086 item_084 item_041 item_023 item_027 item_060 item_053 item_079 
#>        1        1       -1        1       -1        1        1       -1 
#> 
#> $set_B
#> item_025 item_069 item_093 item_072 item_089 item_097 item_076 item_027 
#>       -1       -1        1        1       -1       -1        1       -1 
#> item_053 item_096 item_038 item_034 item_063 item_013 item_082 item_021 
#>        1        1       -1       -1        1        1        1       -1 
#> 
#> $set_C
#> item_050 item_090 item_091 item_072 item_086 item_041 item_060 item_079 
#>       -1       -1        1        1        1       -1        1       -1 
#> item_047 item_095 item_016 item_006 item_039 item_031 item_081 
#>        1        1        1       -1        1       -1        1 
#> 

# example showing empty=NA
setlist2 <- make_venn_test(20, 3, sizes=c(12, 14, 8), do_signed=TRUE)
setlist2 <- lapply(setlist2, function(i){
   i[] <- sample(c(-1, 0, 1),
      replace=TRUE,
      size=length(i));
   i
})
imv2 <- list2im_value(setlist2, empty=NA);
imv2;
#>         set_A set_B set_C
#> item_15     0     1    -1
#> item_19     1    -1     0
#> item_14     1     1    NA
#> item_03    -1    -1    NA
#> item_10     0    -1     1
#> item_02     0    NA    NA
#> item_06    -1    NA    NA
#> item_11     0     0    NA
#> item_05    -1     1    NA
#> item_04    -1    -1    NA
#> item_18     0     0     1
#> item_09     1     0     0
#> item_08    NA     0    NA
#> item_07    NA     1     1
#> item_17    NA     1    NA
#> item_01    NA     0    NA
#> item_12    NA    NA     0
#> item_13    NA    NA     1

# to convert back to list, define empty=NA so 0 is not considered as empty
im_value2list(imv2, empty=NA);
#> $set_A
#> item_15 item_19 item_14 item_03 item_10 item_02 item_06 item_11 item_05 item_04 
#>       0       1       1      -1       0       0      -1       0      -1      -1 
#> item_18 item_09 
#>       0       1 
#> 
#> $set_B
#> item_15 item_19 item_14 item_03 item_10 item_11 item_05 item_04 item_18 item_09 
#>       1      -1       1      -1      -1       0       1      -1       0       0 
#> item_08 item_07 item_17 item_01 
#>       0       1       1       0 
#> 
#> $set_C
#> item_15 item_19 item_10 item_18 item_09 item_07 item_12 item_13 
#>      -1       0       1       1       0       1       0       1 
#> 

# make a simple character vector list
setlistv <- lapply(setlist, function(i){
   j <- letters[i+3];
   names(j) <- names(i);
   j;
})
imv <- list2im_value(setlistv);
print(head(imv));
#>          set_A set_B set_C
#> item_067 "b"   ""    ""   
#> item_042 "d"   ""    ""   
#> item_050 "d"   ""    "b"  
#> item_043 "b"   ""    ""   
#> item_014 "b"   ""    ""   
#> item_025 "d"   "b"   ""   

# convert back to list of character vectors
im_value2list(imv);
#> $set_A
#> item_067 item_042 item_050 item_043 item_014 item_025 item_090 item_091 
#>      "b"      "d"      "d"      "b"      "b"      "d"      "b"      "b" 
#> item_069 item_093 item_057 item_009 item_072 item_026 item_007 item_099 
#>      "b"      "d"      "d"      "b"      "d"      "d"      "b"      "b" 
#> item_089 item_083 item_036 item_078 item_097 item_076 item_015 item_032 
#>      "b"      "d"      "d"      "d"      "d"      "d"      "b"      "d" 
#> item_086 item_084 item_041 item_023 item_027 item_060 item_053 item_079 
#>      "d"      "d"      "b"      "d"      "b"      "d"      "d"      "b" 
#> 
#> $set_B
#> item_025 item_069 item_093 item_072 item_089 item_097 item_076 item_027 
#>      "b"      "b"      "d"      "d"      "b"      "b"      "d"      "b" 
#> item_053 item_096 item_038 item_034 item_063 item_013 item_082 item_021 
#>      "d"      "d"      "b"      "b"      "d"      "d"      "d"      "b" 
#> 
#> $set_C
#> item_050 item_090 item_091 item_072 item_086 item_041 item_060 item_079 
#>      "b"      "b"      "d"      "d"      "d"      "b"      "d"      "b" 
#> item_047 item_095 item_016 item_006 item_039 item_031 item_081 
#>      "d"      "d"      "d"      "b"      "d"      "b"      "d" 
#>