&与&&的区别
You can use the
&
operator to evaluate AND across a vector.
The&&
version of AND only evaluates the first member of a vector. Let’s test both for practice.
Type the expression TRUE & c(TRUE, FALSE, FALSE).TRUE & c(TRUE, FALSE, FALSE)
[1] TRUE FALSE FALSETRUE && c(TRUE, FALSE, FALSE)
[1] TRUE
is.TRUE()
只有输入为“TRUE”时,才会返回TRUE。
The function isTRUE() takes one argument. If that argument evaluates to TRUE, the function will return TRUE. Otherwise, the function will return FALSE. Try using this function by typing: isTRUE(6 > 4)
identical()
The function identical() will return TRUE if the two R objects passed to it as arguments are identical. Try out the identical() function by typing: identical(‘twins’, ‘twins’);
identical() will only evaluate to TRUE if its arguments are exactly the same.
>>
1: identical(5 > 4, 3 < 3.1)
2: identical(4, 3.1)
3: identical(‘hello’, ‘Hello’)
4: !identical(7, 7)
Selection: 1
xor()
You should also be aware of the xor() function, which takes two arguments. The xor() function stands for
| exclusive OR. If one argument evaluates to TRUE and one argument evaluates to FALSE, then this function will
| return TRUE, otherwise it will return FALSE. Try out the xor() function by typing: xor(5 == 6, !FALSE)
xor(5 == 6, !FALSE)
[1] TRUE
| That’s a job well done!
|============================================================================ | 75%
| 5 == 6 evaluates to FALSE, !FALSE evaluates to TRUE, so xor(FALSE, TRUE) evaluates to TRUE. On the other hand
| if the first argument was changed to 5 == 5 and the second argument was unchanged then both arguments would
| have been TRUE, so xor(TRUE, TRUE) would have evaluated to FALSE.
which()
We can use the resulting logical vector to ask other questions about ints. The which() function takes a
| logical vector as an argument and returns the indices of the vector that are TRUE. For example which(c(TRUE,
| FALSE, TRUE)) would return the vector c(1, 3).
any() and all()
Like the which() function, the functions any() and all() take logical vectors as their argument. The any()
| function will return TRUE if one or more of the elements in the logical vector is TRUE. The all() function
| will return TRUE if every element in the logical vector is TRUE.