You can try the following command:
> x<-c(1,2,3,-5) > x[x>0] [1] 1 2 3
would return all positive values.
To replace negative numbers, use NA
> x <- ifelse(x<0, NA,x) > x [1] 1 2 3 NA
Another way to select positive values ββwould be to use sign
x[sign(x) == 1]
and we can combine them in Filter
Filter(function(i) i > 0, x) Filter(function(i) sign(i) == 1, x)
source share