0

I'm beginning with R and I have a question. I have this:

x <- data.frame(x0=c(1:10), x1=c("z", "a","a","a","a","a","c","b","b","b"))

So basically two columns. I want to sort alphabetically taking the entire row of the data frame. So that 1 - z (both x0 and x1) appear at the end.

I've tried sort() but just managed to sort the column x1 and not both x0 and x1.

Thanks

rlsrls
  • 1
  • 1

1 Answers1

0

In base R you can subset and order:

x[order(x$x1),]
   x0 x1
2   2  a
3   3  a
4   4  a
5   5  a
6   6  a
8   8  b
9   9  b
10 10  b
7   7  c
1   1  z

With dplyr you use arrange:

library(dplyr)
x %>%
  arrange(x1)
Chris Ruehlemann
  • 10,258
  • 2
  • 9
  • 18