在数据框中添加名称为max列的新列 [英] Add new column with name of max column in data frame

查看:123
本文介绍了在数据框中添加名称为max列的新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个例子 df

df <- dput(structure(list(arts = structure(c(1L, 1L, 3L, 4L), .Label = 
c("art1", 
"art2"), class = "character"), scr1 = c(52L, 58L, 40L, 62L), scr2 = c(25L, 
23L, 55L, 26L), scr3 = c(36L, 60L, 19L, 22L)), .Names = c("art_id", 
"scr1", "scr2", "scr3"), row.names = c(NA, -4L), class = "data.frame"))

> df
  art_id scr1 scr2 scr3
1      1   52   25   36
2      1   58   23   60
3      3   40   55   19
4      4   62   26   22

我使用 dplyr 来总结 art_id

df %>% 
  group_by(art_id) %>% 
  summarise_each(funs(sum))

  art_id  scr1  scr2  scr3
   <int> <int> <int> <int>
1      1   110    48    96
2      3    40    55    19
3      4    62    26    22

我的问题:如何添加另外一个列,名为 top_r ,其中包含最大值之间的列名称 SRC1:SRC3 。结果df将如下所示:

My question: How can I add another column called top_r that contains the column name from the maximum among src1:src3. The resultant df would look like:

  art_id  scr1  scr2  scr3  top_r
   <int> <int> <int> <int>  <char>
1      1   110    48    96   scr1  
2      3    40    55    19   scr2  
3      4    62    26    22   scr1  

我很乐意使用 dplyr ,所以如果有一个答案,使用该库是美好的!

I am comfortable using dplyr so if there's an answer that uses that library that's wonderful!

推荐答案

这将工作:

df %>%
  group_by(art_id) %>%
  summarise_each(funs(sum)) %>%
  mutate(top_r=apply(.[,2:4], 1, function(x) names(x)[which.max(x)]))

# A tibble: 3 × 5
  art_id  scr1  scr2  scr3 top_r
   <int> <int> <int> <int> <chr>
1      1   110    48    96  scr1
2      3    40    55    19  scr2
3      4    62    26    22  scr1

这篇关于在数据框中添加名称为max列的新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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