This is a minor nuisance in R that I'm looking to see if there is possibly some default case for.
I create the following vector:
x <- c(1, 2, 1, NA)
I now want to select from x
, only values equal to 1. I do as such:
x2 <- x[x == 1]
Now, when you see what's in x2
it has the values:
> x2
[1] 1 1 NA
It seems that R defaults to include NA values regardless of the condition. I would like it so that R by default excludes NA values from conditions (as it is true that NA
does not satisfy the condition x == 1
).
I'm aware of the complete.cases
function, used as such:
x2 <- x[complete.cases(x == 1)]
The desired output would be the result of the complete.cases
method as such:
[REMOVED CAUSE I MESSED THIS UP]
Which solves my problem, but I am curious to see if there is a setting in options
or something like that where I can default R to not include NAs in a boolean condition.
I would like to see if there is a way to set are so that x2 <- x[x == 1]
results in the same as x2 <- x[complete.cases(x == 1)]
. Currently the difference is that the non-complete.cases (normal) method allows NAs through and I would like that to not be the case.
Hey, sorry, I realized I messed up my output with complete.cases
as many of you have said, I essentially want to see if I can make this:
> x <- c(1, 2, 1, NA)
> x2 <- x[x == 1 & !is.na(x)]
> x2
[1] 1 1
Work with just this: x2 <- x[x == 1]
. Can I make it so that R automatically ignores NAs. I could create a function to do this, but wanted to see if there is something built in R for single conditions that ignore NAs.
Do you need which?
> x <- c(1, 2, 1, NA)
> x[which(x==1)]
[1] 1 1
To explain, which(x==1)
will give you the locations in your vector x
that matches the test, x==1
. You use this result to subset x
, giving the output.
> which(x==1)
[1] 1 3