查找包含R中行的最大值的所有列的索引?

时间:2020-03-26 16:11:26

标签: r

为了找到我使用的每一行的最大值:

arrayA = [{id: 20, name: 'Jason' }, { id: 15, name: 'Harry' }]

所以我有每行最大值的列表,但是现在我想查找每行出现该最大值的每一列的索引(即,每行都有不同的最大值,可能会出现多次) 。

如何在R中做到这一点?预先感谢!

2 个答案:

答案 0 :(得分:1)

是否可以简单地使用which.max()然后指定列或行? 这不是您想要的方式,但是可能会更简单?

答案 1 :(得分:0)

尝试一下:

# data frame with 5 columns and 10 rows
dat <- as.data.frame(matrix(sample(1:5, 50, replace = T), nrow = 10))
# name for rows
rownames(dat) <- letters[1:10]
# find the max positons by row
apply(dat, 1, function(x) which(x %in% max(x)))
# find the max positons by col
apply(dat, 2, function(x) which(x %in% max(x)))