如何通过增加R数据框架中的权重来打破关系?

时间:2014-02-06 06:11:46

标签: r statistics aggregate

我需要通过眼睛颜色反向按字母顺序排序,breaking ties by increasing weight并找出谁是上面的第四个男性! aggregate是否具有sort功能? R中的break the ties by increasing weight是什么意思?你能举个例子来答案吗?

这是数据框:

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

4 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

使用xtfrm函数对<{3}}进行小修改 -

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

答案 2 :(得分:1)

有几乎肯定比这更简单的方法,但我认为这是有效的。我不得不两次使用order才能让它发挥作用。

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

如果你想把重量排在另一个方向,我认为这样做:

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

答案 3 :(得分: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