添加一个“等级"列到数据框 [英] Add a "rank" column to a data frame

查看:42
本文介绍了添加一个“等级"列到数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含不同年份不同项目计数的数据框:

I have a dataframe with counts of different items, in different years:

df <- data.frame(item = rep(c('a','b','c'), 3),
                 year = rep(c('2010','2011','2012'), each=3),
                 count = c(1,4,6,3,8,3,5,7,9))

我想添加一个year.rank"列,它给出了一个项目在给定年份内的排名,更高的计数会导致更高的排名".有了上面的内容,它看起来像:

And I would like to add a "year.rank" column, which gives an item's rank within a given year, where a higher count leads to a higher "rank". With the above, it would look like:

  item year count year.rank
1    a 2010     1         3
2    b 2010     4         2
3    c 2010     6         1
4    a 2011     3         2
5    b 2011     8         1
6    c 2011     3         3
7    a 2012     5         3
8    b 2012     7         2
9    c 2012     9         1

我知道我可以使用 order(df$count) 对整个数据框执行此操作,但我不确定我将如何按年份执行此操作.

I know I could do this for the whole data frame using order(df$count), but I'm not sure how I would do it by year.

推荐答案

有一个 rank 函数可以帮助您:

There is a rank function to help you with that:

transform(df, 
          year.rank = ave(count, year, 
                          FUN = function(x) rank(-x, ties.method = "first")))
  item year count year.rank
1    a 2010     1         3
2    b 2010     4         2
3    c 2010     6         1
4    a 2011     3         2
5    b 2011     8         1
6    c 2011     3         3
7    a 2012     5         3
8    b 2012     7         2
9    c 2012     9         1

这篇关于添加一个“等级"列到数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆