Skip to main content
more succinct title, fixed grammar, removed code formatting of non-code
Source Link
Henrik
  • 68k
  • 15
  • 153
  • 166

How to sort Sort (order) thedata frame rows of a dataframe by multiple column(s)columns

I want to sort a data.frame frame by multiple columns. For example, with the data.frame frame below I would like to sort by column z'z' (descending) then by column b'b' (ascending):

dd <- data.frame(b = factor(c("Hi", "Med", "Hi", "Low"), levels = c("Low", "Med", "Hi"), ordered = TRUE), x = c("A", "D", "A", "C"), y = c(8, 3, 9, 9), z = c(1, 1, 1, 2)) dd b x y z 1 Hi A 8 1 2 Med D 3 1 3 Hi A 9 1 4 Low C 9 2 

How to sort (order) the rows of a dataframe by multiple column(s)

I want to sort a data.frame by multiple columns. For example, with the data.frame below I would like to sort by column z (descending) then by column b (ascending):

dd <- data.frame(b = factor(c("Hi", "Med", "Hi", "Low"), levels = c("Low", "Med", "Hi"), ordered = TRUE), x = c("A", "D", "A", "C"), y = c(8, 3, 9, 9), z = c(1, 1, 1, 2)) dd b x y z 1 Hi A 8 1 2 Med D 3 1 3 Hi A 9 1 4 Low C 9 2 

Sort (order) data frame rows by multiple columns

I want to sort a data frame by multiple columns. For example, with the data frame below I would like to sort by column 'z' (descending) then by column 'b' (ascending):

dd <- data.frame(b = factor(c("Hi", "Med", "Hi", "Low"), levels = c("Low", "Med", "Hi"), ordered = TRUE), x = c("A", "D", "A", "C"), y = c(8, 3, 9, 9), z = c(1, 1, 1, 2)) dd b x y z 1 Hi A 8 1 2 Med D 3 1 3 Hi A 9 1 4 Low C 9 2 
edited title
Link
Gregor Thomas
  • 147.4k
  • 22
  • 185
  • 320

How to sort (order) the rows of a dataframe by multiple column(s)

edited title
Link
NelsonGon
  • 13.3k
  • 7
  • 32
  • 60

How to sort a dataframe by multiple column(s)?

edited tags
Link
Machavity
  • 31.8k
  • 27
  • 97
  • 108
Loading
edited tags; edited title
Link
smci
  • 34.2k
  • 21
  • 118
  • 152
Loading
Question Protected by zx8754
deleted 6 characters in body
Source Link
Jaap
  • 83.7k
  • 36
  • 190
  • 203
Loading
deleted 4 characters in body
Source Link
Hack-R
  • 23.5k
  • 15
  • 82
  • 138
Loading
edited title
Link
user227710
  • 3.2k
  • 20
  • 36
Loading
edited title
Link
zx8754
  • 56.7k
  • 12
  • 131
  • 229
Loading
edited tags
Link
Ari B. Friedman
  • 73.1k
  • 35
  • 183
  • 238
Loading
code formatting
Source Link
Marek
  • 51k
  • 15
  • 109
  • 125
Loading
Source Link
Christopher DuBois
  • 43.6k
  • 23
  • 74
  • 93
Loading