합집합,교집합,차집합 in r

Published by onesixx on

A <- c("a","b","c","d","e")
B <- c(            "d","e","f")
 
> unique(c(A,B)) 
#[1] "a" "b" "c" "d" "e" "f"
> intersect(A,B)
#[1] "d" "e"
> A[!(A%in%B)]
#[1] "a" "b" "c"
> B[!(B%in%A)]
#[1] "f"

2 vector

A <- c(1,2,3,4,5)
B <-       c(4,5,6,7,8)

합집합 (A∪B)

> unique(c(A,B))
[1] 1 2 3 4 5 6 7 8

교집합(A∩B)

> A[A%in%B]
> B[B%in%A]

> intersect(A,B)
[1] 4 5

차집합(A-B)

> A[!(A%in%B)]
[1] 1 2 3

Multi-vector

https://www.r-bloggers.com/intersect-for-multiple-vectors-in-r/

Reduce(intersect, list(a,b,c))

Reduce(union, list(x, y, z))

reduce()
combine from the left

> a <- c(1:3)
> b <- c(3:5)
> c <- c(5:7)
> Reduce(union, list(a,b,c))
[1] 1 2 3 4 5 6 7

Categories: R Reshaping

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x