Find minimum non-zero value in a column R
Asked Answered
M

3

10

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.

Mera answered 2/9, 2014 at 3:35 Comment(2)
I take it we are to assume that there are no negative values?Selfabuse
You could also do sort. vals <- sort(my_data_frame[, my_column_number]); vals[!!vals][1]Velarde
S
9

That should do the trick.

 min(my_data_frame[my_data_frame$my_column_number>0,my_column_number])
Spud answered 2/9, 2014 at 3:41 Comment(1)
Thanks heaps, as always the solution is obvious to the pros, still getting my head around setting limits in RMera
S
22

If you use a vector:

min(myvector[myvector > 0])
Symbolic answered 26/7, 2015 at 14:52 Comment(0)
S
9

That should do the trick.

 min(my_data_frame[my_data_frame$my_column_number>0,my_column_number])
Spud answered 2/9, 2014 at 3:41 Comment(1)
Thanks heaps, as always the solution is obvious to the pros, still getting my head around setting limits in RMera
A
1
min(my_data_frame[,1][which(my_data_frame[,1]>0)])
Admissive answered 18/12, 2018 at 17:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.