Subsetting a vector with a condition (excluding NA)
Asked Answered
O

4

6
vector1 = c(1,2,3,NA)
condition1 = (vector1 == 2)
vector1[condition1]
vector1[condition1==TRUE]

In the above code, the condition1 is "FALSE TRUE FALSE NA", and the 3rd and the 4th lines both gives me the result "2 NA" which is not I expected.

I wanted elements whose values are really '2', not including NA.

Could anybody explain why R is designed to work in this way? and how I can get the result I want with a simple command?

Oriflamme answered 18/4, 2018 at 2:30 Comment(0)
B
4

The subset vector[NA] will always be NA because the NA value is unknown and therefore the result of the subset is also unknown. %in% returns FALSE for NA, so it can be useful here.

vector1 = c(1,2,3,NA)
condition1 = (vector1 %in% 2)
vector1[condition1]
# [1] 2
Blitz answered 18/4, 2018 at 2:56 Comment(0)
A
2

If you are in RStudio and enter

?`[`

You will get the following explanation:

NAs in indexing

When extracting, a numerical, logical or character NA index picks an unknown element and so returns NA in the corresponding element of a logical, integer, numeric, complex or character result, and NULL for a list. (It returns 00 for a raw result.)

When replacing (that is using indexing on the lhs of an assignment) NA does not select any element to be replaced. As there is ambiguity as to whether an element of the rhs should be used or not, this is only allowed if the rhs value is of length one (so the two interpretations would have the same outcome). (The documented behaviour of S was that an NA replacement index ‘goes nowhere’ but uses up an element of value: Becker et al p. 359. However, that has not been true of other implementations.)

Ashraf answered 18/4, 2018 at 2:57 Comment(0)
G
0

try the logical operator in that case,

vector1 = c(1,2,3,NA)
condition1<-(vector1==2 & !is.na(vector1) )
condition1
# FALSE TRUE FALSE FALSE
vector1[condition1]
# 2

& operation returns true when both of the logical operators are True.

Gerda answered 18/4, 2018 at 2:45 Comment(1)
see ?NA for detailsGerda
H
0

identical is "The safe and reliable way to test two objects for being exactly equal. It returns TRUE in this case, FALSE in every other case." (see ?identical)

As it does not compare elementwise comparison you can use it in sapply to compare each element in vector1 to 2. I.e.:

condition1 = sapply(vector1, identical, y = 2)

which will give:

vector1[condition1]
[1] 2
Hunkydory answered 18/4, 2018 at 3:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.