Home > other >  How to select the highest absolut value in a column?
How to select the highest absolut value in a column?

Time:12-09

In the dataframe

Name <- c("Jon", "Jon", "Maria", "Maria","dan","dan","Jon", "Jon", "Maria", "Maria","dan","dan")
tem <- c(-23, -41, 32, 58,30,10,-20,63,-30,-33,55,66)
group <- c("max", "min", "max", "min","max","min","max", "min", "max", "min","max","min")
var <- c("lan", "lan", "lan", "lan","lan","lan","sea", "sea", "sea", "sea","sea","sea")
df <- data.frame(Name, tem,group,var)

I need to find the highest absolute tem by name and by group

for instance

for Jon max, lan is higher than sea, so I keep lan.

for Jon min sea is higher than lan, so I keep sea

desired output

 Jon -23   max lan
Maria  32   max lan
Maria  58   min lan
Jon  63   min sea
dan  55   max sea
dan  66   min sea

CodePudding user response:

Looks about right

do.call(
  rbind,
  by(df,list(df$Name,df$group),function(x){
    x[which.max(abs(x$tem)),]
  })
)

    Name tem group var
11   dan  55   max sea
1    Jon -23   max lan
3  Maria  32   max lan
12   dan  66   min sea
8    Jon  63   min sea
4  Maria  58   min lan

CodePudding user response:

You may use which.max with abs to get index of maximum value.

library(dplyr)

get_abs_max <- function(x) which.max(abs(x))

df %>% group_by(Name, group) %>% slice(get_abs_max(tem)) %>% ungroup

#  Name    tem group var  
#  <chr> <dbl> <chr> <chr>
#1 Jon     -23 max   lan  
#2 Jon      63 min   sea  
#3 Maria    32 max   lan  
#4 Maria    58 min   lan  
#5 dan      55 max   sea  
#6 dan      66 min   sea  

Or the same function in data.table -

library(data.table)

setDT(df)[, .SD[get_abs_max(tem)], .(Name, group)]

CodePudding user response:

df |> mutate(abs_tem = abs(tem)) |> 
      group_by(Name,group) |>  
      dplyr::filter(abs_tem == max(abs_tem)) |> 
      select(-abs_tem)

# A tibble: 6 × 4
# Groups:   Name, group [6]
  Name    tem group var  
  <chr> <dbl> <chr> <chr>
1 Jon     -23 max   lan  
2 Maria    32 max   lan  
3 Maria    58 min   lan  
4 Jon      63 min   sea  
5 dan      55 max   sea  
6 dan      66 min   sea  
  •  Tags:  
  • r
  • Related