R 逻辑运算

&与&&的区别

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 FALSE

TRUE && 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值