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

后端 未结 3 1166
臣服心动
臣服心动 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:51

    If I understand the problem, you can use table to compute the difference in the number of elements in each set and then create a vector based on the difference of those counts (note that this won't necessarily give you the order you gave in your question).

    > diffs <- table(x) - table(factor(y, levels=levels(factor(x))))
    > rep(as.numeric(names(diffs)), ifelse(diffs < 0, 0, diffs))
    [1] 1 1 2 3
    

提交回复
热议问题