wery
wery

Reputation: 1

Rearrange data frame in R

there is a target vector

f=c(3,2,1,4)

and a two columns data frame (df)

2 4
1 2
7 8
9 10

I want to rearrange all rows in the order of the target f vector..

7 8
1 2
2 4
9 10

is there an easy way?

Upvotes: 0

Views: 197

Answers (1)

Marco
Marco

Reputation: 1492

Yes there is:

f <- c(3,2,1,4)
df <- matrix(c(2, 1, 7, 9, 4, 2, 8, 10), nrow=4)
df[f, ]

Upvotes: 2

Related Questions