Search code examples
rdoubledata-wrangling

Conditionally Select and Convert a Double to a list of Factors


This is a bit of a weird problem, not sure where to start with this. I have a double:

, , s0

            A        B        C
1   0.55417205 -0.0200187 0.2409565
3  -0.56678215 -0.0200187 0.4754662
18  1.37831681 -0.0200187 0.4260734
20  0.55175931 -0.0200187 0.4061181
28 -1.12156686 -0.0200187 0.4702297
30  0.06913091 -0.0200187 0.4188555
47  0.95545025 -0.0200187 0.2715263
50  0.07951062 -0.0200187 0.4648517
52  0.16938094 -0.0200187 0.3918239
68  0.12219372 -0.0200187 0.4339989
70 -0.11545481 -0.0200187 0.6075105
82 -0.36738143 -0.0200187 0.4444718
83 -0.63349441 -0.0200187 0.4593321
89  2.17432660 -0.0200187 0.1025435

and I want to convert this, based on the highest values in each column row, to a list of factors. For example, row "1" would be A because that has the highest value 0.55, row "3" would be C because the highest value is 0.47 etc. The end result would look like:

A C A A...C A
Levels: A B C

I've tried starting with data.frame and data.table::as.data.table and neither really get me anywhere. Any help would be appreciated, thank you!


Solution

  • You may try using reshape2::melt

    Data

    dummy <- read.table(text = " row_id           A        B        C
    1   0.55417205 -0.0200187 0.2409565
    3  -0.56678215 -0.0200187 0.4754662
    18  1.37831681 -0.0200187 0.4260734
    20  0.55175931 -0.0200187 0.4061181
    28 -1.12156686 -0.0200187 0.4702297
    30  0.06913091 -0.0200187 0.4188555
    47  0.95545025 -0.0200187 0.2715263
    50  0.07951062 -0.0200187 0.4648517
    52  0.16938094 -0.0200187 0.3918239
    68  0.12219372 -0.0200187 0.4339989
    70 -0.11545481 -0.0200187 0.6075105
    82 -0.36738143 -0.0200187 0.4444718
    83 -0.63349441 -0.0200187 0.4593321
    89  2.17432660 -0.0200187 0.1025435", header = T)
    

    Code

    library(dplyr)
    library(reshape2)
    
    dummy %>%
      melt(id.vars = "row_id") %>%
      group_by(row_id) %>%
      filter(value == max(value)) %>%
      arrange(row_id) %>%
      pull(variable)
    
     [1] A C A A C C A C C C C C C A
    Levels: A B C