-3

Is it possible to easily do nested ordering of the row elements in two columns of a dataframe or matrix?

I have the following dataset:

structure(list(Group = structure(c(2L, 2L, 1L, 1L, 2L, 2L, 1L, 
1L, 2L, 1L, 2L, 1L), .Label = c("A", "B"), class = "factor"), 
    Education = structure(c(5L, 3L, 5L, 6L, 6L, 5L, 2L, 2L, 6L, 
    5L, 3L, 6L), .Label = c("Below NVQ Level 2", "No qualification", 
    "NVQ Level 2", "NVQ Level 4 and above", "NVQ Level3", "Other qualification"
    ), class = "factor"), Full.Time = c(26, 22, 27, 18, 27, 26, 
    9, 9, 27, 27, 22, 18), PT.16.hours = c(21, 21, 24, 18, 14, 
    21, 12, 12, 14, 24, 21, 18), PT.16.hours.1 = c(25, 25, 11, 
    13, 16, 25, 5, 5, 16, 11, 25, 13)), .Names = c("Group", "Education", 
"Full.Time", "PT.16.hours", "PT.16.hours.1"), row.names = c("8", 
"9", "2", "5", "11", "8.1", "6", "6.1", "11.1", "2.1", "9.1", 
"5.1"), class = "data.frame")

which when read into R is as follows:

 DD
     Group           Education Full.Time PT.16.hours PT.16.hours.1
8        B          NVQ Level3        26          21            25
9        B         NVQ Level 2        22          21            25
2        A          NVQ Level3        27          24            11
5        A Other qualification        18          18            13
11       B Other qualification        27          14            16
8.1      B          NVQ Level3        26          21            25
6        A    No qualification         9          12             5
6.1      A    No qualification         9          12             5
11.1     B Other qualification        27          14            16
2.1      A          NVQ Level3        27          24            11
9.1      B         NVQ Level 2        22          21            25
5.1      A Other qualification        18          18            13

I would like to order the rows such that the A's and B's are in order (with the A's first) and then the B's and within each of A and B, I want to get the Education ordered (sorted) in a nested manner. The other columns should maintain the same relationships. I feel like this is an easy question, but how does one do this efficiently ?

Thanks in advance.

user3236841
  • 1,088
  • 1
  • 15
  • 39

1 Answers1

1

In base R that's quite easily achieved with

df[ order( df$Group, df$Education ), ]

     Group           Education Full.Time PT.16.hours PT.16.hours.1
6        A    No qualification         9          12             5
6.1      A    No qualification         9          12             5
2        A          NVQ Level3        27          24            11
2.1      A          NVQ Level3        27          24            11
5        A Other qualification        18          18            13
5.1      A Other qualification        18          18            13
9        B         NVQ Level 2        22          21            25
9.1      B         NVQ Level 2        22          21            25
8        B          NVQ Level3        26          21            25
8.1      B          NVQ Level3        26          21            25
11       B Other qualification        27          14            16
11.1     B Other qualification        27          14            16
vaettchen
  • 7,299
  • 22
  • 41