Find minimum non-zero value in a column R

2019-05-25 00:08发布

问题:

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.

回答1:

That should do the trick.

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


回答2:

If you use a vector:

min(myvector[myvector > 0])


回答3:

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


标签: r min