我在每行的矩阵中搜索最小值

时间:2016-07-28 12:25:22

标签: r

我在R中有一个矩阵:

[,1] [,2] [,3]

“7”“8”“0”......

我希望我的矩阵的每一行都没有零的最小值。 例如:在第1行中,我们得到值7,8和0.结果应为7.

提前谢谢!

1 个答案:

答案 0 :(得分:2)

我们可以rowMins使用matrixStats replaceNA使用na.rm=TRUE,然后使用library(matrixStats) rowMins(replace(m1, m1==0, NA), na.rm =TRUE) 获取预期的最小值每行。

base R

或者如果我们使用apply(m1, 1, FUN = function(x) min(x[x!=0]))

pmin

转换为data.frame后,其他选项为do.call(pmin, c(as.data.frame(replace(m1, m1==0, NA)), na.rm = TRUE))

m1 <- matrix(c(7, 4, 5, 8, 3, 0, 0, 1, 4), ncol=3)

数据

<?php
if(isset($_POST['query'])) {
$no_results = '';
$query = mysql_real_escape_string($_POST['query']);//added some security

    $raw_results = mysql_query("SELECT * FROM evrdata WHERE evr_no = '$query'") or die(mysql_error());

    if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following

        while($results = mysql_fetch_array($raw_results)){            
          $evr_no ='<br/>EVR No. :  '.'<b>'.$results['evr_no'].'</b>';
          $surname ='<br/>Surname :  '.'<b>'.$results['surname'].'</b>';
          $othername ='<br/>First Names :  '.'<b>'.$results['othername'].'</b>';
          $ps_code ='<br/>PS Code :  '.'<b>'.$results['ps_code'].'</b>';

        }
    }
    else{ // if there is no matching rows do following
        $no_results = '<br/>Sorry, but there were no results found. Perhaps re-enter your EVR No.: or double check your entry.</b>';        
    }
}
?>