Jono
Jono

Reputation: 3633

Unique elements of two vectors

I have two vectors and I need to find out the unique elements in both, together.

I tried doing length(summary(merge(v1, v2))) but summary aggregates a bunch of my dataset because there is only one of those entries, so I get an incorrect length.

E.g.:

list_1 <- c(1,2,3,4,5,5,6,1,2,3)

list_2 <- c(2,3,4,5,10,11,10)

and the outcome should be

1,2,3,4,5,6,10,11

P.S. bonus points if you can return all the unique elements in a vector... :-)

Upvotes: 0

Views: 5975

Answers (3)

SabDeM
SabDeM

Reputation: 7190

here is my solution.

p1 <- c(1, 4, 1, 1, 4, 5, 6, 7, 8)
p2 <- c(3, 4, 1, 6, 90, 10, 32)
unique(c(p1, p2))

Upvotes: 5

Amrita Sawant
Amrita Sawant

Reputation: 10913

You can use unlist with union

    unlist(union(a,b))

Upvotes: 1

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193527

It sounds like you're looking for union:

> union(v1, v2)
[1]  1  2  3  4  5  6 10 11

Upvotes: 12

Related Questions