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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With