R/subset_list_of_lists.R

Defines functions subset_list_of_lists

Documented in subset_list_of_lists

#' Subset a list of lists to a list of desired lists only Subset a list of lists
#' down to a list of lists containing only lists with the value at a certain
#' position (with this values indicated in another list)
#'
#' @param list_of_lists a list of lists, such as a list of lists containing SNP
#'   window position, SNPs and scaffold, as generated by
#'   \code{\link{pre_allocate}}
#' @param desired_list list of desired elements
#' @param subindex index of lists within list of lists, which will be checked to
#'   see if the list contains the desired element and should be subsetted
#'
#' @return A list in which all of the values indicated by the `subindex`
#'   parameter are matched by the `desired_list`
#' @export
#'
#' @examples
#' subset_list_of_lists(list_of_lists = small_pre_allocated_windows,
#'                      desired_list = c(14515000, 14491000),
#'                      subindex = 1)
#'
subset_list_of_lists <- function(list_of_lists,
                                 desired_list,
                                 subindex = 1){

  # https://stackoverflow.com/questions/18556703/select-a-subset-of-lists-from-list-of-lists

  subsetted_list_of_lists <- list_of_lists[which(
    sapply(list_of_lists, `[[`, subindex) %in% desired_list)]

  subsetted_list_of_lists
}
naglemi/mtmcskat documentation built on Aug. 23, 2023, 5:35 p.m.