Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find minimum non-zero value in a column R

Tags:

r

min

I have this situation in R:

my_minimum <- min(my_data_frame[,my_column_number])

This returns the minimum value. What I want is the minimum non-zero value. I have seen a lot of more complicated situations where people want a vector of the non-zero minimum values but I simply want a single number, the lowest non-zero value that exists in

my_column_number

within

my_data_frame

For context, this is taking place within a for loop that iteratively plots some stuff for each column, and I need to get the non-zero minimum to add to the plot.

like image 208
user2214046 Avatar asked Sep 02 '14 03:09

user2214046


People also ask

How do you find the minimum value of zero except?

Select a blank cell (H1) for placing the minimum value, enter formula =SMALL(A1:E7,COUNTIF($A$1:$E$7,0)+1) into the Formula Bar, and then press the Enter key. Then the minimum value of specified range excluding zero is populated in the selected cell as above screenshot shown.

How do I find the smallest number in a column in R?

Minimum value of a column in R can be calculated by using min() function. min() Function takes column name as argument and calculates the Minimum value of that column.

How do you find the minimum value of a variable in R?

In R, we can find the minimum or maximum value of a vector or data frame. We use the min() and max() function to find minimum and maximum value respectively. The min() function returns the minimum value of a vector or data frame. The max() function returns the maximum value of a vector or data frame.

How do you find the minimum of a column?

To find the minimum value of a column, use the MIN() aggregate function; it takes the name of the column or expression to find the minimum value. In our example, the subquery returns the minimum value in the temperature column (subquery: SELECT MIN(temperature) FROM weather ).


2 Answers

If you use a vector:

min(myvector[myvector > 0])
like image 196
user5157725 Avatar answered Sep 28 '22 13:09

user5157725


That should do the trick.

 min(my_data_frame[my_data_frame$my_column_number>0,my_column_number])
like image 28
CHP Avatar answered Sep 28 '22 13:09

CHP