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

Time:10-05

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!

CodePudding user response:

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"
  • Related