Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value based on largest value by neighbouring column

Tags:

dataframe

r

dplyr

Using group_by() I want to get the value of column value based on the largest value of column 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
like image 480
safex Avatar asked Dec 28 '25 14:12

safex


1 Answers

We can use which.max to get the index of the value and use that to subset the 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     
like image 55
akrun Avatar answered Dec 31 '25 19:12

akrun