I have a numeric vector in R, which consists of both negative and positive numbers. I want to separate the numbers in the list based on sign (ignoring zero for now), into two seperate lists:
- a new vector containing only the negative numbers
- another vector containing only the positive numbers
The documentation shows how to do this for selecting rows/columns/cells in a dataframe – but this dosen’t work with vectors AFAICT.
How can it be done (without a for loop)?
It is done very easily (added check for NaN):
If you want exclude both NA and NaN, is.na() returns true for both: