问题
I am not trying to solve any particular problem, but trying to learn R and understand its logical negation operator "!" documented on page http://stat.ethz.ch/R-manual/R-devel/library/base/html/Logic.html
It works for me when used in combination with =, in expressions such as:
1 != 2
TRUE
But I can't quite comprehend standalone application of this operator. For instance, can I use it to select elements of the list which do not have specific name. Here's my attempt to do that, but it did not work:
vector1 <- 1:5 # just making vector of 5 numbers
vector2 <- 5:1 # same vector backwards
list <- list(Forward=vector1, Backwards=vector2) # producing list with two elements
x = "Forward"
list[!x]
My output is:
Error in !x : invalid argument type
Will appreciate any hints on where my logic goes wrong in this case, and what are other good uses of this operator except for != case.
Thanks! Sergey
回答1:
First, it's probably best not to think of !=
as !
acting on =
, but rather as a separate binary operator altogether.
In general, !
should only be applied to boolean vectors. So this is probably more like what you are after:
vector1 <- 1:5 # just making vector of 5 numbers
vector2 <- 5:1 # same vector backwards
l <- list(Forward=vector1, Backwards=vector2) # producing list with two elements
x = "Forward"
l[!(names(l) %in% x)]
where names(l) %in% x
returns a boolean vector along the names of the list l
indicating whether they are contained in x
or not. Finally, I avoided the use of list
as a variable, since you can see it is a fairly common function as well.
回答2:
I agree with everything said by the other two posters, but want to add one more thing I always tell when teaching R.
R works in that it evaluates statements from the inside to the outside and each of those statements needs to run on it's own. If you already have an error in an inner statement, no wonder the outers do not produce anything.
In your case one could say you have two statements: !x
and list accessing on list
via [
.
If you replicate R's behavior you notice that !x
already produces the error:
> !x
Error in !x : invalid argument type
Hence, the correct solutions try to change this step.
So: Always check your innermost statements when an errors occurs and work yourself outwards.
回答3:
First, I think the !
in !=
is not the !
operator. It is a distinct, !=
operator, which means "different from".
Second, the !
operator is a logical one, the logical negation, and it must be applied to a logical vector :
R> !(c(TRUE,FALSE))
[1] FALSE TRUE
As numbers can be coerced to logical, it can also be applied to a numerical vector. In this case 0
will be considered as FALSE
and any other value as TRUE
:
R> !c(1,0,-2.5)
[1] FALSE TRUE FALSE
In your example, you are trying to apply this logical operator to a character string, which raises an error.
If you want to subset lists, data frames or vectors by names, indices or conditions, you should read and learn about the indexing part of the R language, which is described in the R manuals and most of the introductory books and documents.
One way to subset a list by names could be, for example :
R> list[!(names(list) %in% "Forward")]
$Backwards
[1] 5 4 3 2 1
来源:https://stackoverflow.com/questions/16157372/how-logical-negation-operator-works