美文网首页
R语言 -- 交并补:intersect、union、setdi

R语言 -- 交并补:intersect、union、setdi

作者: 生信摆渡 | 来源:发表于2020-05-27 17:33 被阅读0次

    R语言 -- 交并补:intersect、union、setdiff、Reduce多重操作

    > # 交并补
    > a = seq(0, 40, 4); a
     [1]  0  4  8 12 16 20 24 28 32 36 40
    > b = seq(0, 40, 5); b
    [1]  0  5 10 15 20 25 30 35 40
    > 
    > # 交集
    > intersect(a,b)
    [1]  0 20 40
    > 
    > # 并集
    > union(a,b)
     [1]  0  4  8 12 16 20 24 28 32 36 40  5 10 15 25 30 35
    > 
    > # a 减去 a交b
    > setdiff(a,b)
    [1]  4  8 12 16 24 28 32 36
    > 
    > # b 减去 a交b
    > setdiff(b,a)
    [1]  5 10 15 25 30 35
    > 
    > # 多个集合之间操作
    > Reduce(union,  list(v1 = c("a","b","c","d"),
    +                         v2 = c("a","b","e"),
    +                         v3 = c("a","f","g"),
    +                         v4 = c("a","f","e","g"))
    + )
    [1] "a" "b" "c" "d" "e" "f" "g"
    > 
    > Reduce(intersect,  list(v1 = c("a","b","c","d"),
    +                     v2 = c("a","b","e"),
    +                     v3 = c("a","f","g"),
    +                     v4 = c("a","f","e","g"))
    + )
    [1] "a"
    > 
    > Reduce(setdiff,  list(v1 = c("a","b","c","d"),
    +                         v2 = c("a","b","e"),
    +                         v3 = c("a","f","g"),
    +                         v4 = c("a","f","e","g"))
    + )
    [1] "c" "d"
    

    相关文章

      网友评论

          本文标题:R语言 -- 交并补:intersect、union、setdi

          本文链接:https://www.haomeiwen.com/subject/yafgahtx.html