R中的数据框基于其他数据框的比较

Dataframe in R based on comparison of others dataframe

我有三个数据集

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

例如

> 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

我想创建一个数据帧,当 A 的值最高时包含 A,当 B 的值最高时包含 B,当 C 的值最高时包含 C。所以在这种情况下它会是这样的:

C B C
C C A
A A A

提前致谢!!

你可以试试 array + apply + which.max

inds <- apply(array(c(A, B, C), c(dim(A), 3)), 1:2, which.max)
inds[] <- c("A", "B", "C")[inds]

这样

> inds
     [,1] [,2] [,3]
[1,] "C"  "A"  "C"
[2,] "B"  "A"  "A"
[3,] "C"  "B"  "B"

数据

> set.seed(1)

> (A <- matrix(rnorm(9), ncol = 3))
           [,1]       [,2]      [,3]
[1,] -0.6264538  1.5952808 0.4874291
[2,]  0.1836433  0.3295078 0.7383247
[3,] -0.8356286 -0.8204684 0.5757814

> (B <- matrix(rnorm(9), ncol = 3))
           [,1]       [,2]        [,3]
[1,] -0.3053884 -0.6212406 -0.04493361
[2,]  1.5117812 -2.2146999 -0.01619026
[3,]  0.3898432  1.1249309  0.94383621

> (C <- matrix(rnorm(9), ncol = 3))
          [,1]        [,2]        [,3]
[1,] 0.8212212  0.78213630  0.61982575
[2,] 0.5939013  0.07456498 -0.05612874
[3,] 0.9189774 -1.98935170 -0.15579551

嗨,它不是很简洁,但下面的代码可以完成这项工作。

    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
    
    ````