How can I get the complement of vector y in vector x

后端 未结 3 1170
臣服心动
臣服心动 2021-02-09 16:21

That\'s x \\ y using mathematical notation. Suppose

x <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,2,1,1,1,3) 
y <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,         


        
3条回答
  •  礼貌的吻别
    2021-02-09 16:39

    Here a solution using pmatch (this gives the "complement" as you require):

    x <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,2,1,1,1,3)
    y <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1)
    res <- x[is.na(pmatch(x,y))]
    

    From pmatch documentation:

    "If duplicates.ok is FALSE, values of table once matched are excluded from the search for subsequent matches."

提交回复
热议问题