Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to choose only positive values?

Tags:

r

I have a dataset which contains positive, negative as well as NA values. How could I select positive-only values using a script? I would also like to replace negatives numbers with NA and leave NA values as they are.

like image 392
Tika Ram Gurung Avatar asked Feb 09 '15 04:02

Tika Ram Gurung


People also ask

How do you make input only accept positive numbers?

You can force the input to contain only positive integer by adding onkeypress within the input tag. Here, event. charCode >= 48 ensures that only numbers greater than or equal to 0 are returned, while the min tag ensures that you can come to a minimum of 1 by scrolling within the input bar.

What numbers are only positive?

Positive numbers include the natural, or counting numbers like 1,2,3,4,5, as well as fractions like 3/5 or 232/345, and decimals like 44.3. Even irrational numbers like pi or the square root of two are positive unless you put a negative sign in front of them. Zero is neither negative, nor positive.


1 Answers

You could use the which function:

sample <- c(1, 2, -7, NA, NaN)
sample[which(sample > 0)]
[1] 1 2

For negative values assign NA.

Using which:

sample[which(sample < 0)] <- NA 
like image 146
codingEnthusiast Avatar answered Oct 18 '22 02:10

codingEnthusiast