How to check if an exact vector is a subset of another list vector

I looked at similar posts and I tried them, but I still could not solve my problem.

I have a list of vectors like

y2 <- c(0,0,NA,0,0,0,0)
y <- c(0,0,0,NA,NA,0)
x <- c(0,0,0,0)
li <-list(y2,y,x)

I am trying to remove a vector from the list liif the exact vector is a subset of the other.

For example, in my case, I should have y2, and yas a result, but xdropped out of the list, as xis the exact subset y2.

I wrote the following script, but it just gives y2 as the result and ignores y:

UniquePath <- function(PATHS)
{
  for(j in length(PATHS):1)
  {
    for(i in 1:length(PATHS))
    {
      if((i!=j)&(isTRUE(all(is.element(PATHS[[j]],PATHS[[i]])))))
      {
        PATHS<-PATHS[-j]
        break
      }
      else
      {
        next
      }
    }
  }
  return(PATHS)
}
+4
source share
3 answers

My approach will be (if your line does not have ";")

# make li unique
li <- unique(li)
# collapse each unique list element to a length-1 string surrounded by ";" 
x <- sapply(li, function(x) paste0(";", paste(x, collapse = ";"), ";"))
# check each element, if this is found somewhere in any other element
li[sapply(seq_along(x), function(i) !any(grepl(x[i], x[-i], fixed = TRUE)))]
# [[1]]
# [1]  0  0 NA  0  0  0  0
# 
# [[2]]
# [1]  0  0  0 NA NA  0

, , OP , , .

: x y , x y - . .

+4

, , .

uniquePath <- function(l){
  idxs <- 1:length(l)
  tmp <- lapply(l, table, useNA='always')
  l2 <- lapply(idxs, function(i){
    res <- l[[i]]
    for(j in idxs[-i]){
      if ( all(res %in% l[[j]]) & all(tmp[[i]] <= tmp[[j]])){ 
        res <- NULL; break 
      }
    }
    res
  })
  Filter(Negate(is.null), l2)
}

:

y2 <- c(0,0,NA,0,0,0,0); y <- c(0,0,0,NA,NA,0); x <- c(0,0,0,0)
li <-list(y2 = y2,y = y,x = x)
## This is your example, where x is subset of both y and y2
uniquePath(li)
# [[1]]
# [1]  0  0 NA  0  0  0  0
#
# [[2]]
# [1]  0  0  0 NA NA  0

x <- c(0,0,0,0); y <- c(0,NA,0,0,0); y2 <- c(NA,NA,0,0,0,0)
l <- list(x,y,y2)
## Here x is a subset of y and y is a subset of y2
uniquePath(l)
# [[1]]
# [1] NA NA  0  0  0  0
0

How about the next use of a recursive function.

# Your sample data
y2 <- c(0,0,NA,0,0,0,0)
y <- c(0,0,0,NA,NA,0)
x <- c(0,0,0,0)
li <-list(y2 = y2,y = y,x = x)

I define a recursive function collapseListthat recursively deletes list entries if they are an ordered subset of other list items.

# Recursive function to collapse entries
collapseList <- function(lst) {
    s <- sapply(lst, paste, collapse = "");
    if (sum(grepl(s[1], s)) > 1) {
        lst <- lst[-1];
        collapseList(lst);
    }
    else lst;
}

Results based on your sample list:

# Order list by number of list elements
li <- li[order(sapply(li, length))];

li <- collapseList(li);
li;
#$y
#[1]  0  0  0 NA NA  0
#
#$y2
#[1]  0  0 NA  0  0  0  0

@ set example

li <- list(
    x = c(0,0,0,0), 
    y = c(0,NA,0,0,0), 
    y2 = c(NA,NA,0,0,0,0));

li <- li[order(sapply(li, length))];
collapseList(li);
#$y
#[1]  0 NA  0  0  0
#
#$y2
#[1] NA NA  0  0  0  0
0
source

Source: https://habr.com/ru/post/1690259/


All Articles