Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 209949

Sort a Dataframe using External Factor Ordering in R

$
0
0

A niggling problem. Simple example. Suppose I have the following:

df <- data.frame(x = as.factor(1:10),
                 y = rnorm(10, 0, 1))

x_order = c("3", "1", "5", "7", "8", "9", "2", "6", "10", "4")

I would like to reorder the rows of df such that the values in df$x appear in the order specified in the x_order vector. Is there an easy way to do this please?

Thank you.


Viewing all articles
Browse latest Browse all 209949

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>