Find minimum non-zero value in a column R

南笙酒味 提交于 2020-01-02 05:47:06

问题


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)])


来源:https://stackoverflow.com/questions/25615439/find-minimum-non-zero-value-in-a-column-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!