Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: returning the minimum positive number

Tags:

r

I have a dataframe with positive and negative numbers. What I need to return is the smallest positive number. Is there a function that does this?

 dfcount  <- data.frame(A=c(1,2,3,4,-5,-6,-7))

ie minpositive(dfcount) returns 1 and not -7

Thank you for your help

like image 213
adam.888 Avatar asked Dec 26 '22 16:12

adam.888


2 Answers

This function would work:

minpositive = function(x) min(x[x > 0])

For example:

dfcount  <- data.frame(A=c(1,2,3,4,-5,-6,-7))
minpositive(dfcount)
# 1
like image 189
David Robinson Avatar answered Dec 28 '22 10:12

David Robinson


This should work:

min(dfcount$A[dfcount$A > 0])
like image 21
Dan Avatar answered Dec 28 '22 10:12

Dan