R:获取矩阵中最小元素的行和列名,但最小!= 0 [英] R: Get the row and column name of the minimum element of a matrix but with minimum != 0

查看:187
本文介绍了R:获取矩阵中最小元素的行和列名,但最小!= 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个矩阵,其中包含很多零和正数值. 我想获取元素所在的行号和列号 矩阵的最小NONZERO值.

I've got a matrix with a lot of zeros and with positive numerical values. I want to get the row and column number for which the element is the minimal NONZERO value of the matrix.

我没有发现min()有额外的选项来排除零,所以我怎么能 处理这个?

I don't find that min() has extra options to exclude zero, so how can I handle this?

推荐答案

似乎答案可能更短,但是您可以将NA替换为零,并使用na.rm = T

Seems like there could be a shorter answer but u can replace the zeros with NA and use na.rm=T

test = matrix(c(0:9,0:9),nrow =4,ncol=5)
test[which(test==0)] = NA
minValue = min(test,na.rm=T) 
rows = which(apply(test,1,min,na.rm=T)==minValue)
cols = which(apply(test,2,min,na.rm=T)==minValue)

允许重复

这篇关于R:获取矩阵中最小元素的行和列名,但最小!= 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆