-1

Thank you very much for spending time helping this.. I have a data set named "MyData" as below: I want to sort the whole data set(A to E) with a specific order with column A,B and C. The order can be defined by me. First sorting by A, with the order: "yellow", "green", then "red" After A is sorting, then sorting by column B, with the order "X", "Z" then "Y" After A and B is sorting, then sorting by C, the order should be the largest number to the smallest number.

     A          B         C          D          E        
1  red         X        0.8        aaaa        111
2  yellow      Y        0.2        dddd        222
3  green       X        0.3        cccc        111
4  yellow      Z        0.6        dddd        333
5  green       Y        0.1        aaaa        123
6  yellow      X        0.5        cccc        324
7  yellow      X        0.4        zzzz        222
8  yellow      X        0.8        bbbb        126

Below is my desired output:

     A         B         C          D          E 
8  yellow      X        0.8        bbbb        126
6  yellow      X        0.5        cccc        324
7  yellow      X        0.4        zzzz        222
4  yellow      Z        0.6        dddd        333
2  yellow      Y        0.2        dddd        222
3  green       X        0.3        cccc        111
5  green       Y        0.1        aaaa        123
1  red         X        0.8        aaaa        111

My actual dataset contains around 100 rows. Thank you very much!!!:)

www
  • 38,575
  • 12
  • 48
  • 84
Miki
  • 1
  • 3
  • It is a bad idea to share your data through images. Please see this link to learn how to make reproducible examples: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – www Apr 12 '18 at 09:55

2 Answers2

1

Or in base R:

df[order(
    factor(df$A, levels = c("yellow", "green", "red")),
    factor(df$B, levels = c("X", "Z", "Y")),
    -df$C), ]
#    A B   C    D   E
#8 yellow X 0.8 bbbb 126
#6 yellow X 0.5 cccc 324
#7 yellow X 0.4 zzzz 222
#4 yellow Z 0.6 dddd 333
#2 yellow Y 0.2 dddd 222
#3  green X 0.3 cccc 111
#5  green Y 0.1 aaaa 123
#1    red X 0.8 aaaa 111

Sample data

df <- read.table(text =
    "     A          B         C          D          E
1  red         X        0.8        aaaa        111
2  yellow      Y        0.2        dddd        222
3  green       X        0.3        cccc        111
4  yellow      Z        0.6        dddd        333
5  green       Y        0.1        aaaa        123
6  yellow      X        0.5        cccc        324
7  yellow      X        0.4        zzzz        222
8  yellow      X        0.8        bbbb        126", header = T)
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
0

We can create factor columns and specify the level to arrange the column. The last mutate_if is optional, which is just converting the factor back to character.

library(dplyr)

dat2 <- dat %>%
  mutate(A = factor(A, level = c("yellow", "green", "red")),
         B = factor(B, c("X", "Z", "Y"))) %>%
  arrange(A, B, desc(C)) %>%
  mutate_if(is.factor, as.character)
dat2
#        A B   C    D   E
# 1 yellow X 0.8 bbbb 126
# 2 yellow X 0.5 cccc 324
# 3 yellow X 0.4 zzzz 222
# 4 yellow Z 0.6 dddd 333
# 5 yellow Y 0.2 dddd 222
# 6  green X 0.3 cccc 111
# 7  green Y 0.1 aaaa 123
# 8    red X 0.8 aaaa 111

DATA

dat <- read.table(text = "     A          B         C          D          E        
1  red         X        0.8        aaaa        111
2  yellow      Y        0.2        dddd        222
3  green       X        0.3        cccc        111
4  yellow      Z        0.6        dddd        333
5  green       Y        0.1        aaaa        123
6  yellow      X        0.5        cccc        324
7  yellow      X        0.4        zzzz        222
8  yellow      X        0.8        bbbb        126",
                  header = TRUE, stringsAsFactors = FALSE)
www
  • 38,575
  • 12
  • 48
  • 84