danwiththeplan
danwiththeplan

Reputation: 173

Find minimum non-zero value in a column R

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.

Upvotes: 10

Views: 23304

Answers (3)

jalaluddin1
jalaluddin1

Reputation: 33

min(my_data_frame[,1][which(my_data_frame[,1]>0)])

Upvotes: 1

user5157725
user5157725

Reputation: 221

If you use a vector:

min(myvector[myvector > 0])

Upvotes: 22

CHP
CHP

Reputation: 17189

That should do the trick.

 min(my_data_frame[my_data_frame$my_column_number>0,my_column_number])

Upvotes: 9

Related Questions