Suppose we have the following database:
ID x y
1 23 -3
2 2 51
3 -2 45
4 NA 2
5 NA -2
6 NA NA
I want to eliminate all the rows with negative values on the columns x and y, but I want to keep the NAs (that are written as the special value in R).
The result should be the following:
ID x y
2 2 51
4 NA 2
6 NA NA
I have tried using:
database <- database[!(database$x < 0 && database$x != "NA" || database$y < 0 && database$y != "NA")]
But it does not work, as it gives the error
'length(x) = 2 > 1' in coercion to 'logical(1)'
Which, from what I have seen, should be a problem of the new R version.
I have also tried using !is.na()
, and substituting &&
and ||
to &
and |
, but it doesn't work.
Does anyone know how to solve this issue?
CodePudding user response:
In your code, you are doing double negation, which is not necessary. Just flip the sign in the comparison statement (i.e. database$x < 0
to database$x > 0
). Also, to index a dataframe, you need database[row, column]
, where in your code, you are missing the index for column.
database[(database$x > 0 | is.na(database$x)) & (database$y > 0 | is.na(database$y)),]
Or with the package dplyr
:
library(dplyr)
database %>% filter(if_all(x:y, ~ .x >0 | is.na(.x)))
Output
ID x y
2 2 2 51
4 4 NA 2
6 6 NA NA