tags:

views:

547

answers:

7

I have a vector x, that I would like to sort based on the order of values in vector y. The two vectors are not of the same length.

x <- c(2, 2, 3, 4, 1, 4, 4, 3, 3)
y <- c(4, 2, 1, 3)

The expected result would be:

[1] 4 4 4 2 2 1 3 3 3
A: 

loop over y and move all matched value in X to correct location.

Henry Gao
+1  A: 
x <- c(2, 2, 3, 4, 1, 4, 4, 3, 3)
y <- c(4, 2, 1, 3)
for(i in y) { z <- c(z, rep(i, sum(x==i))) }

The result in z: 4 4 4 2 2 1 3 3 3

The important steps:

  1. for(i in y) -- Loops over the elements of interest.

  2. z <- c(z, ...) -- Concatenates each subexpression in turn

  3. rep(i, sum(x==i)) -- Repeats i (the current element of interest) sum(x==i) times (the number of times we found i in x).

Godeke
+2  A: 

You could convert x into an ordered factor:

x.factor <- factor(x, levels = y, ordered=TRUE)
sort(x)
sort(x.factor)

Obviously, changing your numbers into factors can radically change the way code downstream reacts to x. But since you didn't give us any context about what happens next, I thought I would suggest this as an option.

Matt Parker
+1  A: 

[Edit: Clearly Ian has the right approach, but I will leave this in for posterity.]

You can do this without loops by indexing on your y vector. Add an incrementing numeric value to y and merge them:

y <- data.frame(index=1:length(y), x=y)
x <- data.frame(x=x)
x <- merge(x,y)
x <- x[order(x$index),"x"]
x
[1] 4 4 4 2 2 1 3 3 3
Shane
+14  A: 

Here is a one liner...

y[sort(order(y)[x])]

[edit:] This breaks down as follows:

order(y)             #We want to sort by y, so order() gives us the sorting order
order(y)[x]          #looks up the sorting order for each x
sort(order(y)[x])    #sorts by that order
y[sort(order(y)[x])] #converts orders back to numbers from orders
Ian Fellows
That is very succinct, but I'm having a hard time figuring out what's going on there. Could you elaborate a bit?
Matt Parker
Thanks! order() was baffling me - seemed to just be rearranging the numbers arbitrarily, but that was just an artifact of having a sequence of 1-4.
Matt Parker
This is pretty and shows a good understanding of R's built-ins. +1
Godeke
+1  A: 

how about

rep(y,table(x)[as.character(y)]) ?

(Ian's is probably still better)

Ben Bolker
+1  A: 

what about this one

x[order(match(x,y))]
gd047