0

I need to sort in reverse alphabetical order by eye color, breaking ties by increasing weight and find out who's the fourth male from above! Does aggregate have a sort function? What does it mean to break the ties by increasing weight in R? Can you please provide the answer with an example?

Here's the data frame:

jalal
   age sex weight eye.color hair.color
1   23   F   93.8      blue      black
2   21   M  180.8     amber       gray
3   22   F  196.5     hazel       gray
4   22   M  256.2     amber      black
5   21   M  219.6      blue       gray
6   16   F  152.1      blue       gray
7   21   F  183.3      gray   chestnut
8   18   M  179.1     brown      blond
9   15   M  206.1      blue      white
10  19   M  211.6     brown      blond
11  20   F  209.4      blue      white
12  21   M  194.0     brown     auburn
13  22   F  204.1     green      black
14  21   F  157.4     hazel        red
15  15   F  238.0     green       gray
16  20   F  154.8      gray       gray
17  16   F  245.8      gray       gray
18  23   M  198.2      gray        red
19  19   M  169.1     green      brown
20  24   M  198.0     green       gray
Mona Jalal
  • 24,172
  • 49
  • 166
  • 311
  • Have you looked at `order`? – Blue Magister Feb 06 '14 at 06:13
  • it means you have to first sort by `eye.color`, if two persons have the same eye color then sort them by `weight`. [possible duplicate](http://stackoverflow.com/questions/1296646/how-to-sort-a-dataframe-by-columns-in-r) – Nishanth Feb 06 '14 at 06:27
  • @Nishanth Where this question seems to differ from the one at the link you provided is that here the data set is first sorted in reverse order on a character variable rather than on a numeric variable. Sorting a character variable in reverse order as the first step turned out to be far more difficult than I expected. Although there are probably easy ways to do it that eluded me. – Mark Miller Feb 06 '14 at 08:40
  • @MarkMiller yes, I agree – Nishanth Feb 06 '14 at 10:03

4 Answers4

2
dat <- read.table(header = TRUE, text = "nr age sex weight eye.color hair.color
1   23   F   93.8      blue      black
2   21   M  180.8     amber       gray
3   22   F  196.5     hazel       gray
4   22   M  256.2     amber      black
5   21   M  219.6      blue       gray
6   16   F  152.1      blue       gray
7   21   F  183.3      gray   chestnut
8   18   M  179.1     brown      blond
9   15   M  206.1      blue      white
10  19   M  211.6     brown      blond
11  20   F  209.4      blue      white
12  21   M  194.0     brown     auburn
13  22   F  204.1     green      black
14  21   F  157.4     hazel        red
15  15   F  238.0     green       gray
16  20   F  154.8      gray       gray
17  16   F  245.8      gray       gray
18  23   M  198.2      gray        red
19  19   M  169.1     green      brown
20  24   M  198.0     green       gray")

require(dplyr)
ndat <- dat %.%
  arrange(desc(eye.color), desc(weight)) %.%
  filter(sex == 'M')

ndat[4,]

  nr age sex weight eye.color hair.color
4 10  19   M  211.6     brown      blond
Vincent
  • 5,233
  • 2
  • 23
  • 31
2

Making small modification to this answer using xtfrm function -

sorted.jalal <- jalal[with(jalal, order(-xtfrm(eye.color), weight)), ]

sorted.jalal[sorted.jalal$sex == "M", ][4,]

  age sex weight eye.color hair.color
8  18   M  179.1     brown      blond
Community
  • 1
  • 1
Nishanth
  • 6,312
  • 5
  • 23
  • 36
1

There are almost surely easier ways to do it than this, but I think this works. I had to use order twice to get it to work.

jalal <- read.table(text = '
   age sex weight eye.color hair.color
  23   F   93.8      blue      black
  21   M  180.8     amber       gray
  22   F  196.5     hazel       gray
  22   M  256.2     amber      black
  21   M  219.6      blue       gray
  16   F  152.1      blue       gray
  21   F  183.3      gray   chestnut
  18   M  179.1     brown      blond
  15   M  206.1      blue      white
  19   M  211.6     brown      blond
  20   F  209.4      blue      white
  21   M  194.0     brown     auburn
  22   F  204.1     green      black
  21   F  157.4     hazel        red
  15   F  238.0     green       gray
  20   F  154.8      gray       gray
  16   F  245.8      gray       gray
  23   M  198.2      gray        red
  19   M  169.1     green      brown
  24   M  198.0     green       gray
', header = TRUE, stringsAsFactors = FALSE)

jalal2 <- jalal[order(jalal$eye.color, -jalal$weight),] # order data frame

jalal3 <- jalal2[order(-1:-nrow(jalal2)),] # reverse order data frame
jalal3

jalal4 <- jalal3[jalal3$sex == 'M',] # select males

jalal4[4,] # select fourth male

  age sex weight eye.color hair.color
8  18   M  179.1     brown      blond

If you want weight sorted in the other direction, I think this does it:

jalal2 <- jalal[order(jalal$eye.color, jalal$weight),] # order data frame

jalal3 <- jalal2[order(-1:-nrow(jalal2)),] # reverse order data frame
jalal3

jalal4 <- jalal3[jalal3$sex == 'M',] # select males

jalal4[4,] # select fourth male

   age sex weight eye.color hair.color
10  19   M  211.6     brown      blond
Mark Miller
  • 11,294
  • 21
  • 69
  • 119
0
> sorted.jalal <- jalal[with(jalal, order(eye.color, -weight, decreasing=TRUE)), ]
> sorted.jalal
   age sex weight eye.color hair.color count
8   21   F  131.7     hazel      brown     1
12  22   M  180.4     hazel   chestnut     1
14  27   M  208.5     hazel       gray     1
5   20   F  142.7     green   chestnut     1
13  20   F  185.9     green     auburn     1
16  20   F  195.7     green     auburn     1
17  22   M  199.1     green        red     1
7   23   M  276.3     green      black     1
1   22   F  149.7      gray      blond     1
3   22   F  166.5      gray      white     1
11  23   F  176.2      gray       gray     1
18  19   F  199.5      gray   chestnut     1
10  23   M  214.1      gray   chestnut     1
20  18   M  143.9     brown   chestnut     1
6   22   M  133.7      blue      black     1
4   21   F  179.8      blue      white     1
9   17   M  194.8      blue      brown     1
19  15   M  267.1      blue      brown     1
15  20   F  187.1     amber      white     1
2   19   M  250.2     amber      white     1
Mona Jalal
  • 24,172
  • 49
  • 166
  • 311