R: Get the row and column name of the minimum element of a matrix but with minimum != 0
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)
Allows for duplicates
Print Column Name of Smallest Element of Each Row in R
A base R
option with apply
apply(b, 1, FUN = function(x) names(x)[which.min(x)])
#[1] "2" "4" "2" "12" "7" "12" "4" "9" "2"
Find 2 or more indices (row and column) of minimum element of a matrix
You can use find():
[rows, columns] = find(A == min(min(A)));
Furthermore, you can also specify how many matches to find:
Nmatches = 1;
[rows, columns] = find(A == min(min(A)), Nmatches);
For completeness, you can also specify which matches to find, as in the first or last N matches, by using the 'first' or 'last' flag.
How to find rows in a matrix with only the minimum element in the first column?
Create a logical vector with the min
of the first column and subset the matrix
out1 <- out[out[,1] == min(out[,1]),]
Minimum element on each row and column
// Minimum in Column
public static void colMin(double[][] n){
double[] result = new double[n.length];
double min = 0;
System.out.println("Column min values: ");
for (int i = 0; i < n.length; ++i) {
min = Integer.MAX_VALUE;
for (int j = 1; j < n[i].length; ++j)
if (n[j][i] < min){
min = n[j][i];
result[i] = min;
}
System.out.printf("%.2f ", result[i]);
System.out.println();
}
}
Just Assign your computed min to result[i] as shown above in my code.
and display.
Cheers.
Pandas locate minimum of DataFrame matrix: index, col
Try this:
df.stack().idxmin()
Out[108]: ('index0', 'col0')
Getting the name of the column with the minimum value for a row in a dataframe
We can use max.col
which will return column number with max
value in each row and to handle NA
s we can replace
them with 0. We use ties.method = "first"
so in case if we have a tie in a row for max value we select the first max always.
soo$max_col <- names(soo)[max.col(replace(soo, is.na(soo),0),ties.method = "first")]
soo
# Mycol1 Mycol2 max_col
#1 11.9 10.2 Mycol1
#2 7.8 2.7 Mycol1
#3 8.2 6.4 Mycol1
#4 14.7 55.1 Mycol2
#5 1.0 1.0 Mycol1
#6 21.7 8.6 Mycol1
#7 0.0 0.0 Mycol1
#8 48.6 0.0 Mycol1
#9 0.0 0.0 Mycol1
#10 2.8 1.9 Mycol1
Index row and index column of the minimum value matrix A in 2nd until 3rd row and 2nd column
Since you are inputting the column index so there is no need to find that again. The better approach would be to just do:
col_ind = 2;
[minimum, row_ind] = min(A(2:3,col_ind));
row_ind = row_ind+1; %1 is added since first row is skipped in above line
In your code,
A(2:3, 2)
has only 1 column. Socolumns
will always be1
.- If
A(1,2)
is also the same as the minimum value, your code will return the row index of that first value. - The fact that the first row and column are skipped in calculations is not incorporated in outputs.
Fixing above problems:
[rows,columns]=find(A(2:3,2)==min(A(2:3,2)));
rows=rows+1; columns=columns+1;
Related Topics
Programmatically Creating Markdown Tables in R with Knitr
Passing Several Arguments to Fun of Lapply (And Others *Apply)
Can Dplyr Join on Multiple Columns or Composite Key
Return Data Subset Time Frames Within Another Timeframes
Venn Diagram Proportional and Color Shading with Semi-Transparency
R Plotting Confidence Bands with Ggplot
How to Use the Switch Statement in R Functions
Why Use As.Factor() Instead of Just Factor()
Accept Http Request in R Shiny Application
How to Test If List Element Exists
Plot a Function with Ggplot, Equivalent of Curve()
How to Change the Figure Caption Format in Bookdown
Cbind 2 Dataframes with Different Number of Rows