Search code examples
rdataframecomparison

Dataframe in R based on comparison of others dataframe


I have three datasets

A = matrix( rnorm(9), ncol=3)
B = matrix( rnorm(9), ncol=3)
C = matrix( rnorm(9), ncol=3)

For example

> A
           [,1]       [,2]       [,3]
[1,] -0.4428679 0.01448152 -0.8224422
[2,] -1.3690073 0.56250367  1.2021395
[3,]  0.8035294 0.42666372  1.2945448

> B
             [,1]       [,2]       [,3]
[1,] -0.003198928  1.0213131 -0.7793514
[2,] -1.657098221  0.9322505 -1.4665007
[3,]  0.583796919 -0.9133222 -2.8539670

> C
            [,1]       [,2]        [,3]
[1,]  0.06293484 -0.6532648 -0.55455478
[2,] -0.04065755  1.0875654 -0.01349773
[3,]  0.50026019 -0.3151500  0.63410977

I want to create a dataframe containing A when the value of A is the highest, B when the value of B is the highest and C when the value of C is the highest. So in this case it would be something like:

C B C
C C A
A A A

Thank you in advance!!


Solution

  • Hi it is not very succinct but the code below will do the job.

        library(tidyverse) 
        
        A = matrix(rnorm(9), ncol=3)
        B = matrix(rnorm(9), ncol=3)
        C = matrix(rnorm(9), ncol=3)
        
        # convert to a data frame, 1 column per matrix
        
        df <- data.frame("A" = as.vector(A), "B" = as.vector(B), "C" = as.vector(C)) %>%
        # use if_else to find the highest column in each row   
        mutate(max = if_else(A > B & A > C, "A", 
                               if_else(B > A & B > C, "B",
                                       if_else(C > A & C > B, "C",
        # If there is no single max value or something goes wrong "Draw or Error" will be returned
                                               "Draw or Error"))))
        
        # take the max row from the dataframe and turn it back into a matrix
        result_matrix <- matrix(df$max, nrow = nrow(A))
        
        # return the output
        result_matrix
        
        ````