Find the maximum and minimum value of every column and then find the maximum and minimum value of every row

前端 未结 4 784
离开以前
离开以前 2020-12-13 19:04

I\'ve got this matrix:

a <- matrix(rnorm(1000 * 18, mean = 100, sd = sqrt(10)), 1000, 18)

I would like to find the maximum and minimum v

4条回答
  •  情深已故
    2020-12-13 19:13

    You can try

    apply(a, 1, range)
    

    Using this together with t, this gives you two columns. The first one with the minimum the second with the maximum of the rows.

    head(t(apply(a, 1, range)))
             [,1]     [,2]
    [1,] 95.75922 103.6956
    [2,] 93.62636 106.3934
    [3,] 92.70567 106.9190
    [4,] 96.53577 104.4971
    [5,] 96.61573 107.6691
    [6,] 95.56239 105.5887
    

    for the column maxima change 1 to 2 in the apply function.

提交回复
热议问题