hellter
hellter

Reputation: 1004

Keep unique ranked values from 2 character vectors in R

I have two character vectors of length 5:

x <- c("B","A","D","K","F")
y <- c("A","C","D","F","E")

The characteristics of these vectors are the following:

  1. Each vector is ordered (for example, in vector x element "B" is more important than "A")
  2. There are no duplicates inside each vector, but there may be duplicates amongst them (as it's the case in this example)

What I want is to "merge" these 2 vectors in order to obtain a new vector z of length 5 which contains the most important non duplicate elements of vectors x and y, respectively, giving a priority to x in case of ties.
To be clearer, the logic of my program should work as follows:

  1. Take the first element of x and y: if they're different, put them inside z
  2. Take the second element of x and y: if there's a value that is already inside z, ignore it. Else, put it in.
    Another way to see this is: if there's a value which is stored in an higher position either in x or y, ignore it. Else, put it inside z.
  3. Go to the next position and perform 2 again, comparing each value to all the values in higher positions both in x and y.
  4. Stop when length(z)==5

In my example it would work as follows:

  1. Put B and A inside z
  2. Put C inside z
  3. Put D inside z (which is now of length 4)
  4. We have now values K and F: if we put both, length(z) will be 6, so take just K as it's inside x (priority)

The resulting vector would be z = c("B","A","C","D","K").

I could transform the vectors in lists or whatever, but I would like to avoid using loops.

Upvotes: 0

Views: 78

Answers (1)

Eric Lecoutre
Eric Lecoutre

Reputation: 1481

Here is a solution:

x <- c("B","A","D","K","F")
y <- c("A","C","D","F","E")
df=(t(data.frame(x,y)))
(result=unique(as.vector(df))[1:5])
[1] "B" "A" "C" "D" "K"

Thanks for nice detailed explanation.

Upvotes: 1

Related Questions