Fluffy
Fluffy

Reputation: 17

R - how to reorder rows according to variable containing row order sequence?

maybe this is very simple but I'm stuck and hoping you could help?

I have a variable

Name
a
b
c
d
e
...

And a variable that indicates which rows should be read in what order, e.g.

Row_order
4
5
1
3
2

So my desired outcome table would look like

new_Names
d
e
a
b
c

Is there a way to achieve this?

Thank you!

Upvotes: 0

Views: 96

Answers (1)

Ottie
Ottie

Reputation: 1030

You can use the Row_order vector to index the Name vector (pick values in order):

Name <- letters[1:5]
Row_order <- c(4, 5, 1, 3, 2)
Name[Row_order]
[1] "d" "e" "a" "c" "b"

Upvotes: 1

Related Questions