基于相邻列的最大值的值

时间:2019-08-05 19:30:24

标签: r dplyr

使用group_by()我想基于列value的最大值来获取列value2的值:

df = data.frame(id = c(1,1,1,1,2,2,2,2),
                value = c(4,5,1,3,1,2,3,1),
                value2 = c("a","b","c","d","e","f","g","h"))

df %>% group_by(id) %>%
   sumarise(value2_of_largest_value = f(value, value2))

1  b
2  g

2 个答案:

答案 0 :(得分:4)

我们可以使用which.max来获取value的索引,并使用它来对value2进行子集化

library(dplyr)
f1 <- function(x, y) y[which.max(x)]
df %>%
   group_by(id) %>%
   summarise(value2 = f1(value, value2))
   #or simply
   # summarise(value2 = value2[which.max(value)]) 
# A tibble: 2 x 2
#     id value2
#  <dbl> <fct> 
#1     1 b     
#2     2 g     

答案 1 :(得分:1)

library(dplyr) df1 %>% group_by(id) %>% filter(value == max(value)) 中的另一种方法:

data.table

或在library(data.table) setDT(df1)[setDT(df1)[, .I[value == max(value)], by=id]$V1] 中:

cl_hard_wired_encryptor