【问题标题】:Identify rows that meet condition and store in matrix识别满足条件的行并存储在矩阵中
【发布时间】:2020-01-14 08:13:54
【问题描述】:

我需要识别矩阵中满足条件的行。我将问题设置如下。总体而言,目标是确定 1)特定列中的前两个条目是什么,以及 2)它们对应于哪些行。然后我想将相应的行存储在 2xn 矩阵中。

Mat1 <- data.frame(matrix(nrow = 10, ncol =250, data = rnorm(250,0,1)))
seq1 <- seq(1, 247,3)

 Mat1[,1:4]
            X1         X2          X3           X4
1   0.39560216 -1.2391890  1.00771944 -0.225181006
2  -0.92136335 -0.5042209  0.51758214 -0.008936688
3  -0.67657261  1.3167817 -0.22997139 -1.478361654
4  -1.94389531  0.7944302 -0.16763378 -1.847748926
5   0.11998316  0.4850342 -2.47604164 -0.846030811
6   1.26607727  2.3710318 -0.60115423  1.255747735
7  -1.09798680 -0.2817050  0.03150861 -1.350501958
8   0.43790646  0.1989955  1.22612459  0.323815132
9   0.61639304  0.8102352 -0.69921481  0.118795023
10  0.01786964 -0.1222586 -1.50414879  0.649616182


所以在第 1 列 (seq1[1]) 中,前两个条目是 1.266077 和 0.616393。这些对应于第 6 行和第 5 行。在第 4 列中,前两个条目是 1.2557477 和 0.6496162。这些对应于第 6 行和第 10 行。我想对 seq1 中的所有元素重复此过程。我想将输出存储在一个 2 x 长度(seq1)的矩阵(比如输出)中。第一行应该对应最大值,第二行应该是第二高的值。

【问题讨论】:

  • 使用 set.seed 以便我们重现您的示例数据。
  • apply(Mat1[seq1], 2, function(x) sort(x, decreasing = TRUE)[1:2])
  • 对于 idices apply(Mat1[,seq1], 2, function(x) order(x, decreasing = TRUE)[1:2])
  • @GKi 快速问题,如果我想识别所有其他未包含的行,我会怎么做?所以在 column1 的情况下,所有不是 9 或 3 的行号
  • sapply(Mat1[,seq1], function(x) order(x, decreasing = TRUE)[-(1:2)])。我应该写答案而不是评论吗?

标签: r matrix conditional-statements rows


【解决方案1】:

你可以试试这样的:

set.seed(2) # "fix" your random numbers due reproducibility
Mat1 <- data.frame(matrix(nrow = 10, ncol =250, data = rnorm(250,0,1)))
seq1 <- seq(1, 247,3)

# select the interesting columns
Mat2 <- Mat1[,c(seq1)]

# create a matrix with the row names of the top 2 values for each interesting column
dat <- sapply(Mat2, function(x) head(row.names(Mat2)[order(x, decreasing = TRUE)], 2)   
class(dat)
[1] "matrix"

dat[,1:4]
     X1  X4  X7  X10
[1,] "9" "3" "2" "7"
[2,] "3" "1" "5" "2"

【讨论】:

    【解决方案2】:

    你可以这样做:

    M <- read.table(header=TRUE, text=
    "X1         X2          X3           X4
    0.39560216 -1.2391890  1.00771944 -0.225181006
    -0.92136335 -0.5042209  0.51758214 -0.008936688
    -0.67657261  1.3167817 -0.22997139 -1.478361654
    -1.94389531  0.7944302 -0.16763378 -1.847748926
    0.11998316  0.4850342 -2.47604164 -0.846030811
    1.26607727  2.3710318 -0.60115423  1.255747735
    -1.09798680 -0.2817050  0.03150861 -1.350501958
    0.43790646  0.1989955  1.22612459  0.323815132
    0.61639304  0.8102352 -0.69921481  0.118795023
    0.01786964 -0.1222586 -1.50414879  0.649616182")
    
    M <- as.matrix(M)
    M
    
    my12 <- function(x) { m <- which.max(x); x[m] <- -Inf; c(m, which.max(x)) };
    apply(M, 2, my12)
    # > apply(M, 2, my12)
    #      X1 X2 X3 X4
    # [1,]  6  6  8  6
    # [2,]  9  3  1 10
    

    获取值(例如最大值):

    I <- apply(M, 2, my12)
    M[cbind(I[1,], 1:ncol(M))]
    

    如果M 是一个数据框,您可以使用sapply(M, my12) ...

    【讨论】:

      【解决方案3】:

      您可以使用sapplyordersubsetting ([1:2]) 获取索引:

      tt <- sapply(Mat1[,seq1], function(x) order(x, decreasing = TRUE)[1:2])
      #or
      tt <- sapply(Mat1[,seq1], order, decreasing = TRUE)[1:2,]
      

      以及带有以下内容的值:

      matrix(Mat1[matrix(c(tt, rep(seq1, each=2)), ncol = 2)], 2)
      #or
      sapply(Mat1[,seq1], function(x) sort(x, decreasing = TRUE)[1:2])
      

      您可以通过以下方式获取所有其他行的索引,但不能获取最大的两个行:

      sapply(Mat1[,seq1], order, decreasing = TRUE)[-(1:2),]
      

      【讨论】:

        猜你喜欢
        • 2011-07-20
        • 1970-01-01
        • 2020-02-11
        • 1970-01-01
        • 2019-12-22
        • 2022-07-30
        • 2011-10-02
        • 2021-12-15
        相关资源
        最近更新 更多