Search code examples
rtidyversedata-wrangling

Concatenating and indexing highest values by grouped columns


I have the following dataset:

A1 A2 A3 A4 A5 A6 A7
-1.2650612 -0.6868529 -0.4456620 1.2240818 0.3598138 0.4007715 0.1106827
colnames<-c(paste0(1:7,"A"))
set.seed(123)
values<-c(rnorm(7))

I want to be able to create a code that will find the highest value (row-wise)found within the first 3 columns (A1,A2,A3) and write the corresponding column name, repeat the same for the following 2 columns and the same for the remaining 2 columns (A6,A7), if all values are the same (maybe it can happen then just write the name of the first columns within the group)

My desired output will be:

A1,A2,A3 A4,A5 A6,7
A3 A4 A5

What can I try next?


Solution

  • Here is one option

    library(dplyr)
    library(tibble)
    library(tidyr)
    pivot_longer(df1, everything()) %>%
      group_by(group = rep(1:3, c(3, 2, 2))) %>% 
      summarise(name1 = name[which.max(value)],
       name2 = toString(name)) %>% 
     select(name2, name1) %>% 
     deframe %>% 
     as_tibble_row
    

    -output

    # A tibble: 1 × 3
      `A1, A2, A3` `A4, A5` `A6, A7`
      <chr>        <chr>    <chr>   
    1 A3           A4       A6      
    

    data

    df1 <- structure(list(A1 = -1.2650612, A2 = -0.6868529, A3 = -0.445662, 
        A4 = 1.2240818, A5 = 0.3598138, A6 = 0.4007715, A7 = 0.1106827), 
    class = "data.frame", row.names = c(NA, 
    -1L))