&& only returns the first response whereas & returns a vector of all responses. In the context of dplyr, you want the whole vector or else something like filter would only return the first row for which the statement is TRUE, instead of all rows that are TRUE. For example, in something like the following with a single &, c(-1,1) > 0 & c(2,-2) >0 you get FALSE FALSE. This is because it is evaluating along the vector looking at index 1 of both vectors, then index 2. So the first statement is -1 > 0 & 2 > 0 ==> FALSE because the first part (-1 is not greater than 0). Then the second statement is 1 > 0 & -2 > 0 ==> FALSE because the second part (-2 is not greater than 0) In contrast, the following just gives FALSE because it only looks at index 1 (the first statement) and ignores everything else c(-1,1) > 0 && c(2,-2) >0
Thanks for the tips, dr Kim.
I have a question: why does && not work under dplyr context? afaik, only & works
&& only returns the first response whereas & returns a vector of all responses. In the context of dplyr, you want the whole vector or else something like filter would only return the first row for which the statement is TRUE, instead of all rows that are TRUE. For example, in something like the following with a single &,
c(-1,1) > 0 & c(2,-2) >0
you get FALSE FALSE. This is because it is evaluating along the vector looking at index 1 of both vectors, then index 2. So the first statement is -1 > 0 & 2 > 0 ==> FALSE because the first part (-1 is not greater than 0). Then the second statement is 1 > 0 & -2 > 0 ==> FALSE because the second part (-2 is not greater than 0)
In contrast, the following just gives FALSE because it only looks at index 1 (the first statement) and ignores everything else
c(-1,1) > 0 && c(2,-2) >0