楼主: chinaqianchao
25088 6

[问答] 请问如何获取矩阵中每一行的最大值对应的列名? [推广有奖]

  • 0关注
  • 0粉丝

高中生

65%

还不是VIP/贵宾

-

威望
0
论坛币
3 个
通用积分
0
学术水平
1 点
热心指数
2 点
信用等级
0 点
经验
193 点
帖子
19
精华
0
在线时间
28 小时
注册时间
2012-6-4
最后登录
2017-10-18

楼主
chinaqianchao 发表于 2013-7-10 11:35:33 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
如果有下列矩阵,如何根据每一行的最大值找到对应列名呢?请大侠帮忙!
             A                        B                C                      D
1  3.773964e-05 2.236609e-07 1.204275e-06 9.999608e-01
2  7.805035e-07 1.026306e-10 1.507551e-07 9.999991e-01
3  5.553381e-06 2.985323e-08 1.000433e-06 9.999934e-01
4  7.893392e-07 9.274931e-11 1.441299e-07 9.999991e-01
5  3.000040e-04 2.510037e-02 2.350496e-04 9.743646e-01


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:最大值 如何 最大值

沙发
hugebear 发表于 2013-7-10 12:30:57
x <- matrix(c(3.773964e-05, 2.236609e-07, 1.204275e-06, 9.999608e-01,
              7.805035e-07, 1.026306e-10, 1.507551e-07, 9.999991e-01,
              5.553381e-06, 2.985323e-08, 1.000433e-06, 9.999934e-01,
              7.893392e-07, 9.274931e-11, 1.441299e-07, 9.999991e-01,
              3.000040e-04, 2.510037e-02, 2.350496e-04, 9.743646e-01),
             nrow = 5, ncol = 4, byrow = TRUE)
dimnames(x) <- list(1:5, LETTERS[1:4])
apply(x, 1, function(t) colnames(x)[which.max(t)])
已有 3 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
958163533 + 1 + 1 + 1 + 1 精彩帖子
ryusukekenji + 1 热心帮助其他会员
qoiqpwqr + 20 + 1 + 1 热心帮助其他会员

总评分: 论坛币 + 21  学术水平 + 2  热心指数 + 3  信用等级 + 1   查看全部评分

藤椅
chinaqianchao 发表于 2013-7-10 15:07:00
hugebear 发表于 2013-7-10 12:30
x
谢谢你,问题解决!
学习下apply()函数

板凳
cocolong 发表于 2013-7-12 11:04:20
> x <- matrix(c(3.773964e-05, 2.236609e-07, 1.204275e-06, 9.999608e-01,
+               7.805035e-07, 1.026306e-10, 1.507551e-07, 9.999991e-01,
+               5.553381e-06, 2.985323e-08, 1.000433e-06, 9.999934e-01,
+               7.893392e-07, 9.274931e-11, 1.441299e-07, 9.999991e-01,
+               3.000040e-04, 2.510037e-02, 2.350496e-04, 9.743646e-01),
+             nrow = 5, ncol = 4, byrow = TRUE)
> x
             [,1]         [,2]         [,3]      [,4]
[1,] 3.773964e-05 2.236609e-07 1.204275e-06 0.9999608
[2,] 7.805035e-07 1.026306e-10 1.507551e-07 0.9999991
[3,] 5.553381e-06 2.985323e-08 1.000433e-06 0.9999934
[4,] 7.893392e-07 9.274931e-11 1.441299e-07 0.9999991
[5,] 3.000040e-04 2.510037e-02 2.350496e-04 0.9743646
> dimnames(x) <- list(1:5, LETTERS[1:4])
> x
             A            B            C         D
1 3.773964e-05 2.236609e-07 1.204275e-06 0.9999608
2 7.805035e-07 1.026306e-10 1.507551e-07 0.9999991
3 5.553381e-06 2.985323e-08 1.000433e-06 0.9999934
4 7.893392e-07 9.274931e-11 1.441299e-07 0.9999991
5 3.000040e-04 2.510037e-02 2.350496e-04 0.9743646
> apply(x, 1, function(t) colnames(x)[which.max(t)])
  1   2   3   4   5
"D" "D" "D" "D" "D"

报纸
cocolong 发表于 2013-7-12 11:05:45
apply {base}        R Documentation
Apply Functions Over Array Margins

Description

Returns a vector or array or list of values obtained by applying a function to margins of an array or matrix.

Usage

apply(X, MARGIN, FUN, ...)
Arguments

X       
an array, including a matrix.

MARGIN       
a vector giving the subscripts which the function will be applied over. E.g., for a matrix 1 indicates rows, 2 indicates columns, c(1, 2) indicates rows and columns. Where X has named dimnames, it can be a character vector selecting dimension names.

FUN       
the function to be applied: see ‘Details’. In the case of functions like +, %*%, etc., the function name must be backquoted or quoted.

...       
optional arguments to FUN.

Details

If X is not an array but an object of a class with a non-null dim value (such as a data frame), apply attempts to coerce it to an array via as.matrix if it is two-dimensional (e.g., a data frame) or via as.array.

FUN is found by a call to match.fun and typically is either a function or a symbol (e.g. a backquoted name) or a character string specifying a function to be searched for from the environment of the call to apply.

Value

If each call to FUN returns a vector of length n, then apply returns an array of dimension c(n, dim(X)[MARGIN]) if n > 1. If n equals 1, apply returns a vector if MARGIN has length 1 and an array of dimension dim(X)[MARGIN] otherwise. If n is 0, the result has length 0 but not necessarily the ‘correct’ dimension.

If the calls to FUN return vectors of different lengths, apply returns a list of length prod(dim(X)[MARGIN]) with dim set to MARGIN if this has length greater than one.

In all cases the result is coerced by as.vector to one of the basic vector types before the dimensions are set, so that (for example) factor results will be coerced to a character array.

References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.

See Also

lapply and there, simplify2array; tapply, and convenience functions sweep and aggregate.

Examples

## Compute row and column sums for a matrix:
x <- cbind(x1 = 3, x2 = c(4:1, 2:5))
dimnames(x)[[1]] <- letters[1:8]
apply(x, 2, mean, trim = .2)
col.sums <- apply(x, 2, sum)
row.sums <- apply(x, 1, sum)
rbind(cbind(x, Rtot = row.sums), Ctot = c(col.sums, sum(col.sums)))

stopifnot( apply(x, 2, is.vector))

## Sort the columns of a matrix
apply(x, 2, sort)

##- function with extra args:
cave <- function(x, c1, c2) c(mean(x[c1]), mean(x[c2]))
apply(x,1, cave,  c1="x1", c2=c("x1","x2"))

ma <- matrix(c(1:4, 1, 6:8), nrow = 2)
ma
apply(ma, 1, table)  #--> a list of length 2
apply(ma, 1, stats::quantile)# 5 x n matrix with rownames

stopifnot(dim(ma) == dim(apply(ma, 1:2, sum)))

## Example with different lengths for each call
z <- array(1:24, dim=2:4)
zseq <- apply(z, 1:2, function(x) seq_len(max(x)))
zseq         ## a 2 x 3 matrix
typeof(zseq) ## list
dim(zseq) ## 2 3
zseq[1,]
apply(z, 3, function(x) seq_len(max(x)))
# a list without a dim attribute

地板
tanxiaoyan123 发表于 2018-10-12 10:10:17
hugebear 发表于 2013-7-10 12:30
x
请问一下,我现在已知矩阵中的某个元素,想把该元素所在列的列名表示出来,看列名是否在数组(A,B,C,D,E,F,G)中,请问代码应该怎么写?谢谢

7
tanxiaoyan123 发表于 2018-10-12 10:10:46
cocolong 发表于 2013-7-12 11:05
apply {base}        R Documentation
Apply Functions Over Array Margins
请问一下,我现在已知矩阵中的某个元素,想把该元素所在列的列名表示出来,看列名是否在数组(A,B,C,D,E,F,G)中,请问代码应该怎么写?谢谢

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-2-7 23:54